【发布时间】:2021-11-07 10:56:18
【问题描述】:
尝试从网站收集信息时:
DefaultHttpRequester req = new DefaultHttpRequester();
req.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0";
IConfiguration config = Configuration.Default.With(req).WithDefaultLoader().WithDefaultCookies();
var afdas = BrowsingContext.New(config).OpenAsync("https://www.wg-gesucht.de/api/profiles/contact-data/5520144?asset_type=0&asset_id=6839242").Result;
我收到一条错误消息:
<html>
<head></head>
<body>{"type":"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html","title":"Not Acceptable","status":406,"detail":"Cannot honor Accept type specified"}</body>
</html>
基于错误消息:
https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
406 不可接受
请求标识的资源只能生成 具有不可接受的内容特征的响应实体 根据请求中发送的接受标头。
除非它是一个 HEAD 请求,否则响应应该包含一个实体 包含可用实体特征和位置的列表 用户或用户代理可以从中选择最合适的一个。 实体格式由给出的媒体类型指定 内容类型标头字段。取决于格式和 用户代理的能力,选择最合适的 选择可以自动执行。然而,这个规范 没有为这种自动选择定义任何标准。
Note: HTTP/1.1 servers are allowed to return responses which are not acceptable according to the accept headers sent in the request. In some cases, this may even be preferable to sending a 406 response. User agents are encouraged to inspect the headers of an incoming response to determine if it is acceptable.如果响应可能不可接受,用户代理应该暂时 停止接收更多数据并询问用户以进一步决定 行动。
我已经添加了所有可以在 Firefox 中看到的标题:
DefaultHttpRequester req = new DefaultHttpRequester();
req.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0";
req.Headers["Accept"] = "*/*"; // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept
req.Headers["Accept-Encoding"] = "gzip, deflate, br";
req.Headers["Accept-Language"] = "de,en-US;q=0.7,en;q=0.3";
req.Headers["Connection"] = "keep-alive";
req.Headers["Sec-Fetch-Dest"] = "document";
req.Headers["Sec-Fetch-Mode"] = "navigate";
req.Headers["Sec-Fetch-Site"] = "cross-site";
req.Headers["Upgrade-Insecure-Requests"] = "1";
IConfiguration config = Configuration.Default.With(req).WithDefaultLoader().WithDefaultCookies();
var afdas = BrowsingContext.New(config).OpenAsync("https://www.wg-gesucht.de/api/profiles/contact-data/5520144?asset_type=0&asset_id=6839242").Result;
但我仍然遇到同样的错误,那我做错了什么?
【问题讨论】:
-
代码中似乎存在错误。您可以尝试使用正确的配置吗?例如
DefaultHttpRequester req = new DefaultHttpRequester(); req.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0"; IConfiguration Config = Configuration.Default.With(req).WithDefaultLoader().WithDefaultCookies(); var afdas = BrowsingContext.New(Config).OpenAsync("https://www.wg-gesucht.de/api/profiles/contact-data/5520144?asset_type=0&asset_id=6839242").Result;
标签: c# networking webclient anglesharp