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だけでは十分ではないということです。

catch具体的には、 which を処理する必要があります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
}

WebException私はいつも aは の型だと思っていたExceptionので、これらはこのcatchハンドラによってキャッチされます:

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

そうではありません。

そのため、コードが「Request timed out」メッセージをスローし、その原因について何の示唆も示さないようにするには、これらの 2 番目のcatchハンドラーを追加することを忘れないでください。

ところで、私のWeb サービス チュートリアルでは、例外を探して応答ヘッダーに返すコードをお勧めします。

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 に答える