简体   繁体   English

在通过SSL的HTTP CONNECT期间设置用户代理标头?

[英]Set user-agent header during HTTP CONNECT over SSL?

I am using the WebClient class in .NET 2.0 to perform an HTTP POST over SSL. 我正在使用.NET 2.0中的WebClient类来通过SSL执行HTTP POST。

Currently I'm manually setting the user-agent header like so: 目前,我正在手动设置用户代理标头,如下所示:

wc = new WebClient();
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

This works fine, except for when I make the request through a proxy server that does HTTP tunnelling and expects a specific user-agent header in the HTTP CONNECT command. 除我通过代理服务器发出请求(该代理服务器执行HTTP隧道并在HTTP CONNECT命令中期望特定的用户代理标头)时,该方法工作正常。

When a proxy acts as a tunnel for SSL, it initially recieves an HTTP CONNECT which tells it where the client is trying to connect to. 当代理充当SSL的隧道时,它最初会收到一个HTTP CONNECT,该HTTP CONNECT会告诉它客户端尝试连接到的位置。

The problem is that if you set the user-agent header in .NET either via HttpWebRequest.UserAgent, or WebClient.Headers.Add, it does not add it to the initial CONNECT request. 问题在于,如果您通过HttpWebRequest.UserAgent或WebClient.Headers.Add在.NET中设置用户代理标头, 则不会将其添加到初始CONNECT请求中。 It does add it to subsequent SSL traffic, but that's not what is needed. 确实会将其添加到后续的SSL流量中,但这不是必需的。

If this was C++ I would simply call WinHttpOpen() to create the sesson and set the pwszUserAgent param to set the user agent for the whole session. 如果是C ++,则只需调用WinHttpOpen()创建sesson并设置pwszUserAgent参数即可为整个会话设置用户代理。 Unfortunately I cannot find an equivalent in .NET. 不幸的是,我在.NET中找不到等效项。

Can anyone point me in the right direction? 谁能指出我正确的方向? I'm sure that someone else must have come across this problem building client apps in .NET. 我确定在.NET中构建客户端应用程序时,肯定有人遇到了这个问题。

Try with HttpWebRequest (framework 2.0): 尝试使用HttpWebRequest(框架2.0):

HttpWebRequest httpReq = HttpWebRequest.Create(new Uri("www.website.com"));
httpReq.Headers["user-agent"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"; 

I'm using it with Windows Phone 7 sdk and it works. 我在Windows Phone 7 SDK中使用了它,并且可以正常工作。

For more info: http://msdn.microsoft.com/en-gb/library/system.net.httpwebrequest(v=vs.80).aspx 有关更多信息: http : //msdn.microsoft.com/zh-CN/library/system.net.httpwebrequest(v=vs.80).aspx

Setting the proxy should be enough 设置代理应该足够

WebClient client = new WebClient();
client.Headers["User-Agent"] =
    "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
    "(compatible; MSIE 6.0; Windows NT 5.1; " +
    ".NET CLR 1.1.4322; .NET CLR 2.0.50727)";
client.Proxy = new WebProxy(your_proxy_url, true);

your_proxy_url could be like http://proxy.company.com:8080/ your_proxy_url可能类似于http://proxy.company.com:8080/

You will not be able to send the User-Agent header using the HttpWebRequest family of classes. 您将无法使用HttpWebRequest系列类发送User-Agent标头。 The RFCs say that the header is optional (SHOULD rather than MUST) RFC表示标头是可选的(应该而不是必须)

You could add a rule to the proxy to allow connections out with no User-Agent header, or for particular target servers, or finally code your own HTTP Protocol using the .NET Socket Classes. 您可以向代理添加规则,以允许没有User-Agent标头的连接(对于特定的目标服务器),或者最终使用.NET套接字类编写自己的HTTP协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM