";请求超时”;当通过.NET Compact从C#向同一Java服务器发出两个HttpWebRequest时

本文关键字:服务器 Java HttpWebRequest 两个 超时 请求 quot Compact NET | 更新日期: 2023-09-27 18:26:01

我有一个客户端应用程序在.NETCF 3.5设备上的C#上运行,并将其发布到远程的Java servlet。在我对同一servlet的第三次HTTPPOST期间,我收到了一个"请求超时"。例如,如果servlet管理登录到我们的Java服务器,那么客户端的前两次登录尝试将通过(同一客户端设备),当我尝试第三次登录时,它将从服务器返回一个"请求超时"异常。我注意到这种情况经常发生,我无法解决问题。我读到C#默认情况下会在HTTP头中发送请求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;
            }

任何帮助都将不胜感激。谢谢

";请求超时”;当通过.NET Compact从C#向同一Java服务器发出两个HttpWebRequest时

这种行为是由于对WebResponse的异常处理错误造成的。您必须始终处理响应并关闭它。否则,HTTPWebrequest的第三次尝试将失败,超时时间受WinCE限制。

以下源代码将是安全的:

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