How to set host header value for SSL requests using HttpWebRequest

I am trying to use the System.Net.HttpWebRequest class to execute an HTTP GET request to a specific web server for web applications to which we load the load on many servers. To do this, I need to set the host header value for the request, and I was able to achieve this using the System.Net.WebProxy class.

However, this all breaks down when I try to execute GET using SSL. When I try to do this, an HttpWebRequest.GetResponse call throws a System.Net.WebException with an HTTP status code of 400 (Bad Request).

Am I trying to achieve this using HttpWebRequest, or should I look for an alternative way to accomplish what I want?

Here is the code that I used to try to make it all work: -

using System; using System.Web; using System.Net; using System.IO; namespace UrlPollTest { class Program { private static int suffix = 1; static void Main(string[] args) { PerformRequest("http://www.microsoft.com/en/us/default.aspx", "www.microsoft.com"); PerformRequest("https://www.microsoft.com/en/us/default.aspx", ""); PerformRequest("https://www.microsoft.com/en/us/default.aspx", "www.microsoft.com"); Console.WriteLine("Press any key to continue"); Console.ReadKey(); } static void PerformRequest(string AUrl, string AProxy) { Console.WriteLine("Fetching from {0}", AUrl); try { HttpWebRequest request = WebRequest.Create(AUrl) as HttpWebRequest; if (AProxy != "") { Console.WriteLine("Proxy = {0}", AProxy); request.Proxy = new WebProxy(AProxy); } WebResponse response = request.GetResponse(); using (Stream httpStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(httpStream)) { string s = reader.ReadToEnd(); File.WriteAllText(string.Format("D:\\Temp\\Response{0}.html", suffix++), s); } } Console.WriteLine(" Success"); } catch (Exception e) { Console.WriteLine(" " + e.Message); } } } } 
+6
source share
2 answers

I used to use a hacked approach - to have a test exe that modifies my hosts file by entering an entry for the farm name at a specific server address (and issuing ipconfig /flushdns ). After that, requests should be redirected to the desired server, as if it were the only one.

Obviously, this requires administrator access ... I use it as part of an automatic smoke test to get into the farm, as if it were separate machines.

Another thing you can try is something in NLB, maybe in TCL (in case of F5) - maybe add a custom header that NLB understands? (assuming it is re-signing SSL).

+2
source

In .NET 4.0, the host header can be configured regardless of the URL that is required for the request, so I would recommend that you use HttpwebRequest.Host to solve your problem. This (I think) will ship as part of .NET 4.0 beta2. There is no way to rewrite the host header to be different in previous versions of .Net without implementing your own subclass of WebRequest (believe me, it has been tried).

0
source

All Articles