c# - WebHttpRequest fails (500) while postman successfully runs the web GET request
问题描述
I try to get a specific value from a specific site...
the site periodically updating the value using an Ajax call to https://www.plus500.co.il/api/LiveData/FeedUpdate?instrumentId=19
(you can Navigate to the address and see you get the XML response.)
using Postman: sending
GET /api/LiveData/FeedUpdate?instrumentId=19 HTTP/1.1
Host: www.plus500.co.il
Cache-Control: no-cache
Postman-Token: f823c87d-3edc-68ce-e1e7-02a8fc68be7a
I get a valid Json Response...
Though, when i try it from C#:
var webRequest = WebRequest.CreateHttp(@"https://www.plus500.co.il/api/LiveData/FeedUpdate?instrumentId=19");
webRequest.Method = "GET";
using (var response = webRequest.GetResponse())
{...}
The request Fails with Error-Code 403 (Forbidden)
when adding:
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36";
The request Fails with Error-Code 500 (Internal Server Error)
Addition (Edit)
I also initiate with
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls |
SecurityProtocolType.Ssl3;
Also, I Tried Setting a CookieContainer, but the result is the same 500.
Why is Postman/Chrome Successfuly querying this API while C# Webrequest do not?
What is the difference?
解决方案
因此,失败的原因是默认情况下,来自邮递员的客户端请求中包含标头,尽管不是来自 C# 请求。
使用像 Fiddler ( https://www.telerik.com/fiddler ) 这样的程序,您可以查看请求以查看邮递员请求的标头是:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36
然而从 C# 开始
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
像这样填写额外的客户端请求标头可以让它顺利通过:
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
webRequest.Headers.Add("Accept-Encoding", "gzip deflate,br");
webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.9");
推荐阅读
- python - Reading .jmp files with Python on a Mac
- google-maps - 如何在颤动的谷歌地图中使用相机而不是地图移动标记
- web-crawler - 文本提取不适用
- swift - 使用 DispatchGroup 的 Swift 线程
- r - 用空格替换功能名称中的所有下划线
- python - 成功解压缩 zip 后如何终止多进程池?
- php - PHP中的API Pinterest - 登录失败
- assembly - YASM:vmovaps 指令导致分段错误
- python - 连接两个表格相对于单元格中的许多对象
- ios - 将音频文件下载到文件管理器需要从本地路径播放