-1
我打一個網址如下樣本崩潰時無一例外:HttpWebRequest的hiting移動網址
http://mobile.example.com/ip/someProduct-fl-oz/productID
而且我的要求是:行
public static string getMobileHtml(string url)
{
string responseData = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "text/html, application/xhtml+xml, */*";
request.KeepAlive = true;
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
request.Timeout = 10000;
request.Host = "mobile.example.com";
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream);
responseData = myStreamReader.ReadToEnd();
}
response.Close();
}
catch (Exception e)
{
responseData = "An error occurred: " + e.Message;
}
return responseData;
}
和代碼崩潰:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
沒有任何異常只是中斷,同樣的事情發生在Curl Lib
,WebClient
但我把它改爲HttpWebRequest
,假設它是一個修復。有什麼建議麼?
您的網址是否以「http」開頭?我也建議你閱讀有關「request.host」屬性在這裏http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.host.aspx –
@MauricioGracia是相同的URL輸入在Ie ,FireFox和我使用Fiddler複製了上述請求,所以這一切都可以使它工作。它甚至沒有達到主機部分,因爲我說它在頂部墜毀。 – confusedMind
如果您使用「WebRequest webRequest = WebRequest.Create(url);」仍然失敗? –