【问题标题】:The server committed a protocol violation. Section=ResponseStatusLine in c#服务器违反了协议。 C# 中的 Section=ResponseStatusLine
【发布时间】:2014-05-19 22:06:02
【问题描述】:

我花了一整天的时间来解决这个问题。我有一个自定义网络服务器,并且来自 Chrome 或 POSTman ReST 客户端的请求工作正常。当我在 c# 中使用 webclient 或 httpwebrequest 时,我得到:服务器违反了协议。尝试将 zip 文件传输到客户端时,Section=ResponseStatusLine。

我试过了:

public static bool SetAllowUnsafeHeaderParsing20()
{
    //Get the assembly that contains the internal class
    Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
    if (aNetAssembly != null)
    {
        //Use the assembly in order to get the internal type for the internal class
        Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
        if (aSettingsType != null)
        {
            //Use the internal static property to get an instance of the internal settings class.
            //If the static instance isn't created allready the property will create it for us.
            object anInstance = aSettingsType.InvokeMember("Section", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });
            if (anInstance != null)
            {
                //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
                FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
                if (aUseUnsafeHeaderParsing != null)
                {
                    aUseUnsafeHeaderParsing.SetValue(anInstance, true);
                    return true;
                }
            }
        }
    }
    return false;
}

<system.net>
  <settings>
    <httpWebRequest useUnsafeHeaderParsing="true" />
  </settings>
</system.net>

在 app.config 中。

我也尝试过 keep-alive=false 并且也弄乱了标题/

这是 webrequest,在 client2Downloadfile 调用上失败:

private void sendManifest()
{

    Uri remoteuri = new Uri(Properties.Settings.Default.masterurl);

    SetAllowUnsafeHeaderParsing20();
    using (WebClient client = new WebClient())
    {
        NameValueCollection reqparm = new NameValueCollection();
        reqparm.Add("application", "TestApp");
        reqparm.Add("manifest", manifest);
        try
        {
            byte[] responsebytes = client.UploadValues(Properties.Settings.Default.masterurl, "POST", reqparm);
            string responsebody = Encoding.UTF8.GetString(responsebytes);
            if (responsebody != "")
            {
                using (WebClient client2 = new WebClient())
                {
                    client2.DownloadFile(Properties.Settings.Default.masterurl + "//" + responsebody + "transfer.zip", "c:\\temp.zip");
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
}

可以在以下位置查看网络服务器响应:

http://gplus2.net:9532/97e456f0-9b57-4315-b03d-40a67f76d440/transfer.zip

非常感谢任何帮助,因为我确实已经没有想法了。这显然是一个格式错误的服务器标头,但我已将其保持在最低限度。

【问题讨论】:

    标签: c# webclient


    【解决方案1】:

    我遇到了同样的问题,我使用以下方法解决了它。我创建了一个覆盖 GetWebRequestMethod 的自定义 Web 客户端。

      class CustomWebClient : WebClient
    {
        /// <summary>
        /// Returns a <see cref="T:System.Net.WebRequest" /> object for the specified resource.
        /// </summary>
        /// <param name="address">A <see cref="T:System.Uri" /> that identifies the resource to request.</param>
        /// <returns>
        /// A new <see cref="T:System.Net.WebRequest" /> object for the specified resource.
        /// </returns>
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).KeepAlive = false;
            }
            return request;
        }
    }
    

    然后我像这样以正常方式提出请求

    using (CustomWebClient client = new CustomWebClient())
            {
                client.Headers[HttpRequestHeader.Authorization] = "Basic " + base64String;
                responseData = client.DownloadData(baseUri);
            }
    

    【讨论】:

    • 并使用httpClient.PostAsync ?
    • 我没听懂你。
    【解决方案2】:

    我在这个问题上苦苦挣扎了很长时间,最终问题出在我添加到 IIS 的自定义标头...我从某个地方复制了该值,它包含 {cr}:

    打开 IIS 管理器 -> 转到网站 -> 在内容视图中,选择“响应标头”。

    检查 IIS 上的自定义标头,如果不需要,请删除标头,并查看标头是否具有格式错误的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 2012-07-25
      • 2011-04-19
      • 2012-06-27
      相关资源
      最近更新 更多