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); } } } }
Cleggy
source share