我試圖使用System.Net.HttpWebRequest類來執行HTTP GET請求到一個特定的Web服務器爲我們的負載在衆多服務器平衡的Web應用程序設置SSL請求一個主機頭值。爲了實現這一點,我需要能夠爲請求設置主機頭值,並且我已經能夠通過使用System.Net.WebProxy類來實現此目的。如何使用HttpWebRequest的
但是,當我嘗試使用SSL執行GET時,這一切都會崩潰。當我嘗試這樣做時,對HttpWebRequest.GetResponse的調用將拋出一個System.Net.WebException,其HTTP狀態碼爲400(錯誤請求)。
是什麼,我想用HttpWebRequest的實現可能的,或者我應該尋找執行我想要什麼的另一種方式?
這是我一直在使用,試圖讓這一切工作的代碼: -
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);
}
}
}
}
正品問題:您如何知道您需要主機頭? 「使用SSL時不支持HTTP 1.1主機頭」http://support.microsoft.com/kb/187504。 – russau 2009-08-06 03:46:41
第二個想法:你可以在瀏覽器上通過SSL獲取文件嗎? – russau 2009-08-06 03:52:30
我需要設置一個主機頭,這樣我才能指定確切的服務器命中,如果Web應用程序在多個服務器之間進行負載均衡。此代碼是我正在編寫的Web監視和測試框架的一部分。 是的,我試圖使在瀏覽器中工作的SSL請求,所以問題在於我試圖在代碼中實現類似功能的方式。您鏈接的文章表明Host支持SSL請求的支持在Windows Server 2003 SP1中可用,並且我們所有的服務器都應該完全打補丁。 – Cleggy 2009-08-06 04:03:22