C#请求没有超时

时间:2010-12-21 19:38:10

标签: c# backgroundworker http-status-code-404 protocolexception

我有这个代码在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";

                }
            };

我已对请求发出超时但它不起作用!这是一个错误,还是我在这里做错了什么?

由于

2 个答案:

答案 0 :(得分:0)

404仍然是服务器对客户端请求的响应。您是否尝试停止服务器并查看代码是否捕获了异常。

答案 1 :(得分:0)

您已在rqst.GetResponse().Close();中关闭了响应,然后您尝试使用rqst.GetResponse()访问该流。
只需注释掉rqst.GetResponse().Close();,它应该有效...