BackgroundWorker で実行されるこのコードがあり、サーバーに POST リクエストを送信して応答を取得する必要があります。動作するはずの場合は正常に動作しますが、404 エラーを誘発しようとすると、エラー報告システムがキャッチされません。
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";
}
};
リクエストにタイムアウトを設定しましたが、うまくいきません! これはバグですか、それともここで何か間違っていますか?
ありがとう