在 System.Windows.Forms.WebBrowser 中添加 HTTP 标头和发布数据
我正在尝试使用 System.Windows.Forms.WebBrowser 发出发送 POST 数据和自定义 HTTP 标头的请求。我也想设置请求的用户代理。我怎么能这样做呢?
I'm trying to use the System.Windows.Forms.WebBrowser to make a request that both sends POST data and custom HTTP headers. I'd like to set the user-agent of the request as well. How could I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用此 Navigate 重载发送 POST 数据和其他 HTTP 标头。但是,要完全控制请求表单(包括用户代理)并抑制 IE 默认发送的标头(如果可能的话),需要大量的互操作,因此您最好使用 HttpWebRequest 滚动自己的请求并将输出输入浏览器以某种方式。
以下是有关自定义 Web 浏览器。请注意,这是基于 .NET WebBrowser 控件的 COM 基础。对于除最常见场景之外的所有场景,您都需要浏览控件的 IWebBrowser2 接口,这意味着熟悉 COM 互操作。这并不完全有趣,但也没有那么糟糕。
You can send POST data and additional HTTP headers using this Navigate overload. But to have complete control over the request form including user agent and suppressing headers that IE will send by default would (if even possible) require so much interop that you'd be better off just rolling your own requests using HttpWebRequest and feeding the output into the browser somehow.
Here is some additional information on customizing the WebBrowser. Note that this is based on the COM underpinnings of the .NET WebBrowser control. For all but the most common scenarios you need to go through the control's IWebBrowser2 interface which means getting familiar with COM interop. It's not exactly fun, but it's not that bad.