[英]help in using HttpWebResponse c#
我需要您的幫助,以找到使用HttpWebResponse下載URL的更好方法
我用了下一個代碼
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時對我來說似乎
請幫助我下載該網站的字符串
注意: 請先測試解決方案代碼,然后再進行測試,因為我已經測試了多個解決方案,但沒人能解決問題
在項目中添加對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;
}
在使用HttpWebRequest.Create
方法調用之前,請調用方法SetUnsafeHeaderParsing()
。
它實際上是服務器的問題。 服務器未遵循HTTP規范。 但是,默認情況下,您的.NET客戶端會遵守規范並將其標記為潛在問題。
與Web服務器安裝在同一台計算機上的Skype可能會導致此類問題,因為默認情況下它使用的端口是同一端口(80)。
要更改該端口,請點擊此處: http : //forum.skype.com/index.php?showtopic=31024
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.