1

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.

Duncan Bayne
  • 3,870
  • 4
  • 39
  • 64
  • Has anyone found a solution for this? I'm having the same problem consuming an external WebService in a client that blocks connections based on the UserAgent header. We do not have control over the WebService and HTTPS is mandatory for the WebService. – nflash Jul 29 '13 at 14:43
  • @nflash: I was informed by Microsoft when I spoke to them that this was a 'change in behaviour' - I wasn't given any indication that this would be fixed in service packs or in future versions of Windows. I'm afraid you're on your own. I've since stopped developing for Windows and now use Ruby & Javascript / Coffeescript instead. This sort of problem was one of the reasons I changed. – Duncan Bayne Jul 30 '13 at 04:24
  • Thanks for the info. I guess that for now I'll have to ask the client if they can change their policies because of this "change in behaviour"...Do you know where to find some documentation from Microsoft where this behaviour is described? This way will have a stronger argument to the client. thanks once again. – nflash Jul 30 '13 at 09:39
  • @nflash: I was provided this information by Eric Law, who is (or at least was at the time) a Microsoft employee. Details here: http://stackoverflow.com/questions/2396554/send-user-agent-through-connect-and-post-with-winhttp – Duncan Bayne Jul 30 '13 at 23:43
  • Hi Duncan, I'm in a similar situation. Were you ever able to send useragent with the CONNECT request? – imlokesh May 05 '17 at 16:46

3 Answers3

0

Try with HttpWebRequest (framework 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.

For more info: http://msdn.microsoft.com/en-gb/library/system.net.httpwebrequest(v=vs.80).aspx

miticojo
  • 357
  • 1
  • 2
  • 5
  • When you say that it works ... does it actually send the user-agent header during the CONNECT? I tested this myself and it did not, it only added it to the subsequent SSL traffic. – Duncan Bayne Dec 08 '12 at 21:25
  • It doesn't work here. User Agent still not sent on HTTPs CONNECT. – mqueirozcorreia May 14 '17 at 15:47
0

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/

0

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
  • 55,577
  • 12
  • 114
  • 149