C# Request not timing out
I have this code which runs in a BackgroundWorker, and should make a POST request to the server and get a response. It works fine when it is supposed to work, but when I try to induce a 404 error it doesn't catch the error reporting system.
loginProcess.DoWork += delegate(object s, DoWorkEventArgs args)
{
// loginProcess BackgroundWorker
try
{
// Try to login, if error, report
loginProcess.ReportProgress(10);
String method = "POST";
String postdata = "postdata=test";
String url = "http://localhost/dev/login.php";
loginProcess.ReportProgress(15);
WebRequest rqst = HttpWebRequest.Create(url);
rqst.Timeout = 5000;
((HttpWebRequest)rqst).KeepAlive = true;
loginProcess.ReportProgress(20);
//rqst.Timeout = this.Timeout;
// only needed, if you use HTTP AUTH
//CredentialCache creds = new CredentialCache();
//creds.Add(new Uri(url), "Basic", new NetworkCredential(this.Uname, this.Pwd));
//rqst.Credentials = creds;
rqst.Method = method;
if (!String.IsNullOrEmpty(postdata))
{
//rqst.ContentType = "application/xml";
rqst.ContentType = "application/x-www-form-urlencoded";
loginProcess.ReportProgress(30);
byte[] byteData = UTF8Encoding.UTF8.GetBytes(postdata);
loginProcess.ReportProgress(40);
rqst.ContentLength = byteData.Length;
loginProcess.ReportProgress(50);
using (Stream postStream = rqst.GetRequestStream())
{
loginProcess.ReportProgress(50);
postStream.Write(byteData, 0, byteData.Length);
loginProcess.ReportProgress(60);
postStream.Close();
loginProcess.ReportProgress(70);
rqst.GetResponse().Close();
rqst.GetRequestStream().Close();
}
}
loginProcess.ReportProgress(90);
using (var response1 = rqst.GetResponse())
{
using (var responseStream1 = response1.GetResponseStream())
{
using (var reader1 = new StreamReader(responseStream1))
{
//StreamReader rsps = new StreamReader(rqst.GetResponse().GetResponseStream());
string strRsps = reader1.ReadToEnd();
loginProcess.ReportProgress(95);
loginVars = strRsps;
//rqst.
//reader1.Close();
//rsps.Dispose();
}
args.Result = "SUCCESS";
}
}
}
catch(WebException err)
{
// Catch error and put into err variable
if(err.Status == WebExceptionStatus.ProtocolError)
{
// If something is wrong with protocol
LoginReporting.ErrorName = Convert.ToString(((HttpWebResponse)err.Response).StatusCode);
LoginReporting.ErrorDescription = Convert.ToString(((HttpWebResponse)开发者_运维问答err.Response).StatusDescription);
LoginReporting.ErrorNotes = "Error when logging in, Server returned: " + Convert.ToString(((HttpWebResponse)err.Response).StatusCode);
LoginReporting.ErrorLocation = "LoginRequest.ProtocolError";
args.Result = "ERROR";
//MessageBox.Show(Convert.ToString(((HttpWebResponse)err.Response).StatusCode));
//MessageBox.Show(Convert.ToString(((HttpWebResponse)err.Response).StatusDescription));
}
else
{
args.Result = "ERROR";
}
}
catch(Exception err)
{
// Catch unhandled error
LoginReporting.ErrorName = Convert.ToString(err);
LoginReporting.ErrorDescription = Convert.ToString(err.Message);
LoginReporting.ErrorNotes = "Error when logging in, Server returned: " + Convert.ToString(err.Message);
LoginReporting.ErrorLocation = "LoginRequest.ProtocolError";
args.Result = "ERROR";
}
};
I have put a timeout on the request but it just doesn't work! Is this a bug, or am I doing something wrong here?
Thanks
A 404 is still a response from the server to the client's request. Did you try stopping the server and see if you code catches the exception.
You closed the Response in rqst.GetResponse().Close();
and then you try to access the Stream with rqst.GetResponse()
.
Just comment out rqst.GetResponse().Close();
and it should work...
精彩评论