首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于特定网站返回错误的reqwest Http请求

用于特定网站返回错误的reqwest Http请求
EN

Stack Overflow用户
提问于 2022-08-01 08:17:59
回答 1查看 591关注 0票数 0

我试着用reqwest机箱发送http GET请求。以下代码起作用:

代码语言:javascript
复制
extern crate reqwest;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let resp = reqwest::blocking::get("https://en.wikipedia.org/wiki/Rust_(programming_language)")?
        .text()?;
    println!("{:#?}", resp);
    Ok(())
}

但是当我将URL更改为https://www.mongolbank.mn/

响应体html显示以下错误,而不是我想要的...Description: </b>An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine...内容

  • 发生什么事了?
  • ,我怎么能修好它?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-01 09:49:19

使用tokio运行时和用户代理绕过错误。用户代理您可以使用浏览器的调试工具包从浏览器中抓取它

代码语言:javascript
复制
use reqwest::{self, header};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>>{
    let mut headers = header::HeaderMap::new();
    headers.insert(header::USER_AGENT, 
        header::HeaderValue::from_static("Mozilla/5.0...."));

    let client = reqwest::Client::builder()
        .default_headers(headers)
        .build()?;

    let res = client.get("https://www.mongolbank.mn").send().await?;
    println!("{:#?}", res);
    Ok(())
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73190746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档