G
Guest
Hello all:
I have a method that does a POST to a secured website using HttpWebRequest.
It worked when logging in the site, but it failed with an HTTP prococol
violation error when it is used to retrieve a report.
Error: The underlying connection was closed: The server committed an HTTP
protocol violation.
I have tried to the set useUnsafeHeaderParsing to true in the app config
file, but it still does not work.
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
Please tell me what I did wrong in the following code?
Thanks,
Tom
//-----------------------------CODE-----------------------------
private void WebRequestPost(string url, string postData)
{
try
{
byte[] buffer = Encoding.ASCII.GetBytes(postData);
mWebRequest = WebRequest.Create(url) as HttpWebRequest;
mWebRequest.Method = "POST";
mWebRequest.ContentType = "application/x-www-form-urlencoded";
mWebRequest.ContentLength = buffer.Length;
mWebRequest.CookieContainer = mCookies;
mWebRequest.AllowAutoRedirect = true;
mWebRequest.KeepAlive = false;
mWebRequest.ProtocolVersion = HttpVersion.Version10;
Stream reqstrm = mWebRequest.GetRequestStream();
reqstrm.Write(buffer, 0, buffer.Length);
reqstrm.Flush();
reqstrm.Close();
// Failed on this call.
HttpWebResponse res = (HttpWebResponse)mWebRequest.GetResponse();
Stream resst = res.GetResponseStream();
StreamReader sr = new StreamReader(resst);
Log(sr.ReadToEnd(), "postdata");
resst.Close();
sr.Close();
res.Close();
}
catch (Exception ex)
{
Program.TraceLog("Failed to post the form: " + ex.Message);
}
}
//----------------------------END CODE-------------------------------
I have a method that does a POST to a secured website using HttpWebRequest.
It worked when logging in the site, but it failed with an HTTP prococol
violation error when it is used to retrieve a report.
Error: The underlying connection was closed: The server committed an HTTP
protocol violation.
I have tried to the set useUnsafeHeaderParsing to true in the app config
file, but it still does not work.
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
Please tell me what I did wrong in the following code?
Thanks,
Tom
//-----------------------------CODE-----------------------------
private void WebRequestPost(string url, string postData)
{
try
{
byte[] buffer = Encoding.ASCII.GetBytes(postData);
mWebRequest = WebRequest.Create(url) as HttpWebRequest;
mWebRequest.Method = "POST";
mWebRequest.ContentType = "application/x-www-form-urlencoded";
mWebRequest.ContentLength = buffer.Length;
mWebRequest.CookieContainer = mCookies;
mWebRequest.AllowAutoRedirect = true;
mWebRequest.KeepAlive = false;
mWebRequest.ProtocolVersion = HttpVersion.Version10;
Stream reqstrm = mWebRequest.GetRequestStream();
reqstrm.Write(buffer, 0, buffer.Length);
reqstrm.Flush();
reqstrm.Close();
// Failed on this call.
HttpWebResponse res = (HttpWebResponse)mWebRequest.GetResponse();
Stream resst = res.GetResponseStream();
StreamReader sr = new StreamReader(resst);
Log(sr.ReadToEnd(), "postdata");
resst.Close();
sr.Close();
res.Close();
}
catch (Exception ex)
{
Program.TraceLog("Failed to post the form: " + ex.Message);
}
}
//----------------------------END CODE-------------------------------