问题
I have spent a whole day trying to resolve this. I have a custom webserver and requests to it from Chrome or POSTman ReST client work fine. As soon a s I use webclient or httpwebrequest in c#, I get : The server committed a protocol violation. Section=ResponseStatusLine when trying to transfer a zip file to the client.
I have tried:
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;
}
and
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
in app.config.
I have also tried keep-alive=false and messed with the headers too/
This is the webrequest, which fails on the client2Downloadfile call:
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());
}
}
}
The webserver response can be seen at :
http://gplus2.net:9532/97e456f0-9b57-4315-b03d-40a67f76d440/transfer.zip
Any assistance is greatly appreciated as I have literally run out of ideas. It is obviously a malformed server header, but I have kept it to a minimum.
回答1:
I had the same issue and I solved it using the following method. I created a custom web client that overrides the GetWebRequestMethod.
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;
}
}
Then I made the request in the normal way like this
using (CustomWebClient client = new CustomWebClient())
{
client.Headers[HttpRequestHeader.Authorization] = "Basic " + base64String;
responseData = client.DownloadData(baseUri);
}
回答2:
I struggled with that issue for a long time, and eventually the problem was a custom header that I added to IIS... I copied the value from somewhere and it contained {cr}:
Open IIS Manager -> go to the web site -> on content view, choose "Response Headers".
check the custom headers on IIS, remove headers if they are not needed, and also see if a header has a malformed value.
来源:https://stackoverflow.com/questions/23747973/the-server-committed-a-protocol-violation-section-responsestatusline-in-c-sharp