seanmonstar / reqwest

An easy and powerful Rust HTTP Client

Home Page:https://docs.rs/reqwest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

redirect 307 Unable to return the correct webpage content properly

mingtianquan opened this issue · comments

use std::error::Error;
use reqwest::Client;
fn main() {
test_http();
}
fn test_http() {
// 创建 Tokio 运行时
let rt = tokio::runtime::Runtime::new().unwrap();
// 在运行时中执行异步代码块
let result = rt.block_on(async {
// 构建一个客户端,设置最大重定向次数
let client = Client::builder()
.danger_accept_invalid_certs(true)
.user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36")
.redirect(reqwest::redirect::Policy::limited(10)) // 设置最大重定向次数为 10
.build()
.unwrap(); // 这里可以直接unwrap,因为建立失败时会引发 panic
// 发送请求并获取响应
let resp = client.get("https://login.adxsales.com/en")
.send()
.await?
.text()
.await?;

    Ok::<String, Box<dyn Error>>(resp)
});

// 处理结果
match result {
    Ok(response) => println!("{}", response),
    Err(e) => eprintln!("Error: {}", e.to_string()),
}

}
调试报错Error: error following redirect for url (https://sso.adxsales.com/?command=attach&broker=ac2018&token=cesu7bwsojs4w8kwck4gg8co8&checksum=6e16c1296cfa27ea5e4bc688ea9105b236c0d96800845883b32ae0ae5ce7180f&return_url=https%3A%2F%2Flogin.adxsales.com%2Fen): too many redirects

这个网页清空cookie后访问的话第一次访问307重定向到https://sso.adxsales.com.......然后在307重定向到https://login.adxsales.com/en
最终访问的还是https://login.adxsales.com/en
但是我调试报错Error: error following redirect for url (https://sso.adxsales.com/?command=attach&broker=ac2018&token=cesu7bwsojs4w8kwck4gg8co8&checksum=6e16c1296cfa27ea5e4bc688ea9105b236c0d96800845883b32ae0ae5ce7180f&return_url=https%3A%2F%2Flogin.adxsales.com%2Fen): too many redirects

不好意思添加.cookie_store(true)参数后可以了谢谢