2011-02-24 108 views
0

我需要你在下載使用URL HttpWebResponse找到更好的方式幫助幫助C#

我用下面的代碼

HttpWebResponse myboot = HttpWebRequest.Create("http://www.wwenews.us/m1.php?id=441229").GetResponse() as HttpWebResponse; 
    StreamReader myboot_content = new StreamReader(myboot.GetResponseStream(), Encoding.GetEncoding("windows-1256")); 
    string temp_data = myboot_content.ReadToEnd(); 

但問題說

The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF 

在我嘗試解析時出現在我身上http://www.wwenews.us/m1.php?id=441229

請幫我下載這個網站的字符串

注:目前前測試解決方案的代碼,因爲我已經測試了幾種解決方案,並沒有一個解決問題

+0

我想你的代碼。不是repro!頁面已下載。 – Andrey

+0

請發佈完整的錯誤消息,包括堆棧跟蹤。 –

+0

您也可以使用HttpClient「下載」該頁面。它比HttpWebRequest/HttpWebResponse更容易使用,但缺少一點功能 – Kane

回答

0

參考加入System.Configuration添加到您的項目並添加以下方法。

public static bool SetUnsafeHeaderParsing() 
    { 
     Assembly oAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection)); 
     if (oAssembly != null) 
     { 
      Type oAssemblyType = oAssembly.GetType("System.Net.Configuration.SettingsSectionInternal"); 
      if (oAssemblyType != null) 
      { 

       object oInstance = oAssemblyType.InvokeMember("Section", 
        BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { }); 

       if (oInstance != null) 
       { 
        FieldInfo objFeild = oAssemblyType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance); 
        if (objFeild != null) 
        { 
         objFeild.SetValue(oInstance, true); 
         return true; 
        } 
       } 
      } 
     } 
     return false; 
    } 

給你打電話之前,該方法SetUnsafeHeaderParsing()使用HttpWebRequest.Create方法調用。

它實際上是服務器的問題。服務器沒有遵循HTTP規範。但是,默認情況下,您的.NET客戶端會遵守規範並將其標記爲潛在的問題。

+0

@HCL當我開始編寫答案時,您的答案是一個班輪。我發佈的片段是我一直在使用我的項目的片段。如果你對此感到生氣,我很抱歉...編輯它。 –

+0

謝謝شكرا.. HCL和丹麥汗 – user504363

+0

不知道這裏發生了什麼..用戶[HCL] [1]在我幾分鐘前發佈了類似的解決方案,並且對我的迴應不滿意。因此,我突破了我的解決方案。但是,幾分鐘後,我看到他的回覆,並刪除了他的所有評論。 :$ [1]:http://stackoverflow.com/users/340628/hcl –