views:

252

answers:

2

I am using the WebClient class in .NET 2.0 to perform an HTTP POST over SSL.

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.

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.

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. It does add it to subsequent SSL traffic, but that's not what is needed.

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. Unfortunately I cannot find an equivalent in .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.

A: 

You will not be able to send the User-Agent header using the HttpWebRequest family of classes. The RFCs say that the header is optional (SHOULD rather than MUST)

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.

blowdart
That's pretty much what the folk at Microsoft I asked said too :-(
Duncan Bayne
A: 

Did you ever find a resolution for this? I think the exact same issue is happening for me. Im trying to set the UserAgent string on my WebService but its not getting passed with the initial request through the proxy, so nomatter what i try and access i get 403: Forbidden.

Our client app will not work - surely this has been encountered before by someone else??

mike luce
According to MS there is no resolution, and as of Windows 7 this behaviour is the same in .NET and WinHTTP; see: http://stackoverflow.com/questions/2396554/send-user-agent-through-connect-and-post-with-winhttp
Duncan Bayne
FYI your post should have been a comment, not an answer.
Duncan Bayne