C# 如何查看HttpWebRequest发送的headers
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/935181/
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
How to view the headers sent by HttpWebRequest
提问by Jeremy Stein
I'm scraping a site with HttpWebRequest, but the site is returning an error. The page works fine when I hit it from my browser. I'd like to compare them to see what may be causing the error. I know how to intercept the request from my browser to inspect the headers, but how do I view the data sent by HttpWebRequest?
我正在使用 HttpWebRequest 抓取一个站点,但该站点返回错误。当我从浏览器点击它时,该页面工作正常。我想比较它们以查看可能导致错误的原因。我知道如何拦截来自浏览器的请求以检查标头,但如何查看 HttpWebRequest 发送的数据?
采纳答案by driis
In order to compare what you do in code, and what the browser does, I am sure a HTTP debugging tool such as Fiddler, would be the easiest solution.
为了比较您在代码中执行的操作和浏览器执行的操作,我确信 HTTP 调试工具(例如Fiddler)将是最简单的解决方案。
Fiddler acts as a proxy between client and server, and displays all information sent over the HTTP protocol.
Fiddler 充当客户端和服务器之间的代理,并显示通过 HTTP 协议发送的所有信息。
It is possible that you will need to configure your .NET app to use the proxy that Fiddler provides. This blog postprovides details on the subject.
您可能需要将 .NET 应用程序配置为使用 Fiddler 提供的代理。这篇博文提供了有关该主题的详细信息。
回答by Carl Bergquist
http://www.fiddler2.com/fiddler2/is a great tool for such things.
http://www.fiddler2.com/fiddler2/是处理此类事情的绝佳工具。
回答by Colin Fine
I don't know if there's a general solution. But if you're using Firefox, either of two add-ons will help: Firebug, or LiveHTTPHeaders.
不知道有没有通用的解决办法。但是,如果您使用的是 Firefox,那么两个附加组件中的任何一个都会有所帮助:Firebug 或 LiveHTTPHeaders。
回答by Chris Doggett
The Net panel of Firebugwill show all requests, including headers.
Firebug的网络面板将显示所有请求,包括标头。
EDIT: Saw you already knew how to do it in a browser as soon as I posted. Try the Headers property:
编辑:我一发布就看到您已经知道如何在浏览器中执行此操作。试试 Headers 属性:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.stackoverflow.com");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Console.WriteLine(response.Headers);
回答by user152771
You can get the headers from a HTTPWebRequest via the Headers property. From MSDN: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx
您可以通过 Headers 属性从 HTTPWebRequest 获取标头。来自 MSDN:http: //msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx