wpf 如何从代码模拟 Web 浏览器的 http 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16828688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:53:22  来源:igfitidea点击:

How can I emulate a web browser http request from code?

c#wpfhttp

提问by user2419241

I use C# in my WPF Project. I want to send a GEThttp request to a website, but I want to send it in a way, so that it will look like a request from a browser.
Now I have a program which sends a GETrequest and gets a response. I use WebRequestclass for sending GETrequests.
I know that browsers add some information to their requests like browser name, OS name and the computer name.
My question is how can I add this information to my WebRequest? To what properties all that information (browser name, OS name) should be assigned?

我在 WPF 项目中使用 C#。我想向GET网站发送http 请求,但我想以某种方式发送它,使其看起来像来自浏览器的请求。
现在我有一个发送GET请求并得到响应的程序。我使用WebRequest类来发送GET请求。
我知道浏览器会在他们的请求中添加一些信息,比如浏览器名称、操作系统名称和计算机名称。
我的问题是如何将此信息添加到我的WebRequest? 所有这些信息(浏览器名称、操作系统名称)应该分配给哪些属性?

采纳答案by nickvane

You should use Fiddlerto capture the request that you want to simulate. You need to look at the inspectors > raw. This is an example of a request to the fiddler site from chrome

您应该使用Fiddler来捕获要模拟的请求。您需要查看检查员 > raw。这是从 chrome 向 fiddler 站点发出请求的示例

GET http://fiddler2.com/ HTTP/1.1
Host: fiddler2.com
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
Referer: https://www.google.be/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,nl;q=0.6

You can then set each one of these headers in your webrequest (see http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx).

然后,您可以在 webrequest 中设置这些标头中的每一个(请参阅http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx)。

WebRequest request = (HttpWebRequest)WebRequest.Create("http://www.test.com");      
request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36";

回答by mikey

Generally the information you are interested in (browser, os, etc.) is sent in the "User Agent" header along with the request. You can control the user agent with its property, here:

通常,您感兴趣的信息(浏览器、操作系统等)与请求一起在“用户代理”标头中发送。您可以在此处使用其属性控制用户代理:

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.useragent.aspx

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.useragent.aspx

There may be other differences, I recommend using Fiddler to capture your browser traffic and then compare it to the traffic from your .NET-based web request.

可能还有其他差异,我建议使用 Fiddler 捕获您的浏览器流量,然后将其与基于 .NET 的 Web 请求的流量进行比较。

http://fiddler2.com/

http://fiddler2.com/

Enjoy.

享受。

回答by Shahzad Latif

All such information is sent via header in a web request. You can also add such information in header as key/value pair. However, you have only limited attributes which you can set using WebRequest's header property; many of them are restricted. You can also check the list of restricted header attributes in the following thread: Cannot set some HTTP headers when using System.Net.WebRequest.

所有这些信息都是通过 Web 请求中的标头发送的。您还可以在标题中添加此类信息作为键/值对。但是,您只能使用 WebRequest 的 header 属性设置有限的属性;其中许多受到限制。您还可以在以下线程中检查受限标头属性列表:使用 System.Net.WebRequest 时无法设置某些 HTTP 标头