2

リモートにある Java サーブレットに POST する .NETCF 3.5 デバイスの C# でクライアント アプリケーションを実行しています。同じサーブレットへの 3 回目の HTTP POST 中に「Request Timed out」というメッセージが表示されます。たとえば、サーブレットが Java サーバーへのログインを管理している場合、クライアントからの最初の 2 回のログイン試行は (同じクライアント デバイスで) 成功し、3 回目の試行を試みると、サーバーから「要求がタイムアウトしました」という例外が返されます。サーバ。私はこれが常に起こることに気付きましたが、問題を理解できません。C# はデフォルトで HTTP ヘッダーで Request 100 continue を送信することを読んだので、ServicePointManager を使用してリクエスト 100 を false に設定し、無駄にしました。

このエラーをスローしているコードは次のとおりです。

            serverUrl = url;
            string responseFromServer = "";
            try
            {
                System.Net.ServicePointManager.Expect100Continue = false;
                int tmp = ServicePointManager.DefaultConnectionLimit;
                // Create a request using a URL that can receive a post. 
                request = (HttpWebRequest)WebRequest.Create(url);
                // Set the Method property of the request to POST.
                request.Method = "POST";
                // Create POST data and convert it to a byte array.            
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(url);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";                
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                request.Timeout = (50 * 100);
                request.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy();
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                // Get the response.
                response = (HttpWebResponse)request.GetResponse();
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                responseFromServer = reader.ReadToEnd();
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();

                return responseFromServer;
            }
            catch (Exception WebExp)
            {
                Logging.Instance.Log(Logging.Levels.Error, "Error in DoPost while retrieving : "+url+ " " + WebExp.Message.ToString());
                Logging.Instance.Log(Logging.Levels.Error, WebExp.StackTrace.ToString());
                throw WebExp;
            }

どんな助けでも大歓迎です。ありがとう!

4

1 に答える 1

4

この動作は、WebResponse に関する誤った例外処理によるものです。常に応答を処理して閉じる必要があります。そうしないと、HTTP Webrequest の 3 回目の試行が、WinCE によって制限されるタイムアウトで失敗します。

次のソース コードは安全です。

HttpWebResponse response = null;
try
{
    //...
    response = (HttpWebResponse)request.GetResponse();
    //...
}
catch(Exception e)
{
    // logging, etc.
    throw e;
}
finally
{
    if(response!=null)
    {
        response.Close();
    }
}

于 2012-07-19T08:06:14.000 に答える