2013-08-22 58 views
-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 LibWebClient但我把它改爲HttpWebRequest,假設它是一個修復。有什麼建議麼?

+0

您的網址是否以「http」開頭?我也建議你閱讀有關「request.host」屬性在這裏http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.host.aspx –

+0

@MauricioGracia是相同的URL輸入在Ie ,FireFox和我使用Fiddler複製了上述請求,所以這一切都可以使它工作。它甚至沒有達到主機部分,因爲我說它在頂部墜毀。 – confusedMind

+0

如果您使用「WebRequest webRequest = WebRequest.Create(url);」仍然失敗? –

回答

0

我測試了你的發佈代碼ahd HttpWebRequest是成功創建的,然後在響應行失敗了,但在我的情況下是因爲我在防火牆後面,而我沒有提供任何憑據。

該問題被診斷爲影響您的測試的提琴手,因此關閉它可以解決問題

相關問題