我怎样才能从仿真代码的Web浏览器的http请求? [英] How can I emulate a web browser http request from code?

查看:313
本文介绍了我怎样才能从仿真代码的Web浏览器的http请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#在我的WPF项目。我想送一个 GET http请求到一个网站,但我想送它在某种程度上,这样它看起来就像从浏览器的请求。结果
现在我有一个计划,发送 GET 请求并得到响应。我使用的WebRequest 类用于发送 GET 请求。结果
我知道,浏览器添加一些信息他们的请求像浏览器的名称,操作系统名称和计算机名。结果
我的问题是我怎么能将此信息添加到我的的WebRequest ?为了应分配什么样的属性所有的信息(浏览器名称,操作系统名称)?

I use C# in my WPF Project. I want to send a GET http 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 GET request and gets a response. I use WebRequest class for sending GET requests.
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?

推荐答案

您应该使用的提琴手捕捉到,你要模拟的请求。
你需要看看城管队员>原料。
这是Chrome浏览器的提琴手站点的请求的例子

You should use Fiddler to 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

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

您可以再设置这些标题中的每一个在你的WebRequest(见的 http://msdn.microsoft.com/en-us/library/system.net。 httpwebrequest.aspx )。

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 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";

这篇关于我怎样才能从仿真代码的Web浏览器的http请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆