帮助使用HttpWebResponse c#

时间:2011-02-24 11:52:23

标签: c# httpwebresponse

我需要你帮助找到使用HttpWebResponse下载网址的更好方法

我使用了下一个代码

    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

时出现

请帮我下载本网站的字符串

注意:在呈现之前测试您的解决方案代码,因为我已经测试了几个解决方案,没有人解决问题

2 个答案:

答案 0 :(得分: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客户端将遵守规范并将其标记为潜在问题。

答案 1 :(得分:0)

安装在与Web服务器相同的计算机上的Skype可能会导致此类问题,因为它默认使用相同的端口(80)。

要更改该端口,请在此处查看: http://forum.skype.com/index.php?showtopic=31024