服务器违反了协议.Section= c#中的ResponseStatusLine

本文关键字:中的 ResponseStatusLine Section 协议 服务器 | 更新日期: 2023-09-27 18:04:19

我花了一整天的时间来解决这个问题。我有一个自定义的web服务器和请求它从Chrome或邮差ReST客户端工作良好。当我在c#中使用webclient或httpwebrequest时,我得到:服务器违反了协议。Section=ResponseStatusLine,当试图传输一个zip文件到客户端。

I have try:

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和乱糟糟的头太/

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

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());
        }
    }
}

web服务器的响应可以看到:

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

任何帮助都是非常感激的,因为我真的已经没有主意了。这显然是一个格式错误的服务器头,但我已将其保持在最小。

服务器违反了协议.Section= c#中的ResponseStatusLine

我有同样的问题,我使用以下方法解决了它。我创建了一个覆盖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);
        }

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

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

检查IIS上的自定义标头,删除不需要的标头,并查看标头的值是否不正确。