C# 服务器违反了协议。截面=c中的ResponseStatusLine#
我花了一整天的时间试图解决这个问题。我有一个定制的Web服务器,Chrome或POSTman ReST客户端对它的请求可以正常工作。当我在c#中使用webclient或httpwebrequest时,我得到:服务器违反了协议。尝试将zip文件传输到客户端时,Section=ResponseStatusLine 我试过:C# 服务器违反了协议。截面=c中的ResponseStatusLine#,c#,webclient,C#,Webclient,我花了一整天的时间试图解决这个问题。我有一个定制的Web服务器,Chrome或POSTman ReST客户端对它的请求可以正常工作。当我在c#中使用webclient或httpwebrequest时,我得到:服务器违反了协议。尝试将zip文件传输到客户端时,Section=ResponseStatusLine 我试过: public static bool SetAllowUnsafeHeaderParsing20() { //Get the assembly that contains
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;
}
及
可以在以下位置查看Web服务器响应:
http://gplus2.net:9532/97e456f0-9b57-4315-b03d-40a67f76d440/transfer.zip
非常感谢您的任何帮助,因为我已经没有任何想法了。这显然是一个格式不正确的服务器头,但我已将其限制在最低限度。我遇到了相同的问题,并使用以下方法解决了它。我创建了一个覆盖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;
}
}
我有同样的问题,我用下面的方法解决了它。我创建了一个覆盖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;
}
}
我与这个问题斗争了很长一段时间,最终问题是我添加到IIS的自定义头。。。我从某处复制了值,它包含{cr}: 打开IIS管理器->转到网站->在内容视图上,选择“响应标题”
检查IIS上的自定义标头,如果不需要,请删除标头,并查看标头是否有格式错误的值。我与该问题斗争了很长时间,最终问题是我添加到IIS中的自定义标头。。。我从某处复制了值,它包含{cr}: 打开IIS管理器->转到网站->在内容视图上,选择“响应标题”
检查IIS上的自定义头,如果不需要,则删除头,并查看头是否有格式错误的值。使用
httpClient.PostAsync
?我没有收到您的消息。使用httpClient.PostAsync
?我没有收到您的消息。
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);
}