0
public int loginEmail(string email, string password)
    {
        HttpWebRequest request = null;
        string responseStr = null;
        string Email = email;
        string Pass = password;

        UTF8Encoding encoding = new UTF8Encoding();
        string postData = "PostData";
        byte[] data = encoding.GetBytes(postData);

        request = (HttpWebRequest)WebRequest.Create("url");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.AllowAutoRedirect = false;
        request.KeepAlive = false;
        request.Proxy = null;
        request.ServicePoint.ConnectionLimit = 1000;
        request.ContentLength = data.Length;
        request.Timeout = 5000;
        request.ServicePoint.ConnectionLeaseTimeout = 5000;
        request.ServicePoint.MaxIdleTime = 5000;

        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                responseStr = response.Headers["Set-Cookie"];
            }
        }
        catch
        {
            return 1;
        }

        string[] cooktemp;
        string[] seperatortemp = new string[] { ";" };
        cooktemp = responseStr.Split(seperatortemp, StringSplitOptions.None);

        LoginHeaders[0] = cooktemp[0] + ";";

        return 0;
    }

这段代码运行得很好,但有时请求没有得到响应。当请求没有得到响应时,程序将挂起,最后它会给出一个超时错误,导致程序崩溃。我现在要做的只是捕捉超时错误,以便我可以处理它,但似乎没有任何东西能捕捉到它。

4

3 回答 3

4

它很可能在GetRequestStream(). 该文档明确指出,WebException如果请求的超时期限到期,它可能会抛出。

因此,在您的 try/catch 中包含该代码块,您应该能够捕获它。

于 2013-01-31T20:15:22.090 回答
0

这是一个旧线程,但我今天也遇到了一个问题。

我没有意识到的是,如果您有一个 Web 服务,例如,它试图写入一个被锁定的文件......那么简单的代码try..catch是不够的。

您必须专门有一个catchwhich handles WebExceptions

try
{
    //  Run your web service code
}
catch (WebException ex)
{
    //  Handle a WebException, such as trying to write to a "locked" file on the network
}
catch (Exception ex)
{
    //  Handle a regular Exception
}

我一直认为 aWebException是 的一种Exception,所以这些会被这个catch处理程序捕获:

catch (Exception ex)
{
    //  Handle a regular Exception
}

它没有。

因此,为了避免您的代码抛出“请求超时”消息,并且没有任何关于导致它们的建议,请记住添加这些第二个catch处理程序。

顺便说一句,在我的网络服务教程中,这是我推荐的代码,它查找异常,并在响应标头中返回它们:

try
{
    //  Put your code in here
}
catch (WebException ex)
{
    //  Return any exception messages back to the Response header
    OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
    response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
    response.StatusDescription = ex.Message.Replace("\r\n", "");
    return null;
}
catch (Exception ex)
{
    //  Return any exception messages back to the Response header
    OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
    response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
    response.StatusDescription = ex.Message.Replace("\r\n", "");
    return null;
}
于 2017-01-06T13:13:50.527 回答
-1

System.Net.WebException

try { ... }            
catch (System.Net.WebException sne)
        {
            MessageBox.Show(req.Timeout.ToString());
        }

我认为无论如何超时将永远是“5000”。如果你告诉它“超时为 5 秒”,它总是会在放弃前尝试 5 秒。

于 2016-06-16T23:23:12.037 回答