请求已中止:操作已超时

时间:2013-04-18 11:59:34

标签: c# asp.net .net garbage-collection httpwebrequest

  

只是想在添加此问题中给出的代码后添加,我没有发现此错误再次发生。我认为垃圾收集可以解决问题。

我正在努力解决上述错误。我有两个asp.net Web应用程序。其中一个被视为服务器,另一个被视为客户端。我使用Hessian编码方法来处理Web请求和响应。

最初,我的客户端和服务器100%正确地相互响应。但是一段时间后我的客户端停止联系我的服务器,我收到了这个错误:

  

请求已中止:操作已超时

我已经尝试过很多我在互联网上发现的东西来解决我的问题。我做的第一件事是关闭请求和响应流。

然后我添加了以下属性:

request.KeepAlive = false;
request.Timeout = 5000;
request.Proxy = null;

request.ServicePoint.ConnectionLeaseTimeout = 5000;
request.ServicePoint.MaxIdleTime = 5000;

之后我添加了ReadWriteTime和GC.Collect()。

但我的所有努力都没有结果。

此问题仅发生在GoDaddy服务器上,我的项目在我的本地服务器和我的另一台物理服务器上运行得非常好。从我的localhost运行项目时,我也没有收到此错误。

仅在我部署到GoDaddy服务器或将本地客户端连接到GoDaddy服务器时才会发生。

从日志中我可以看到请求停止到达服务器端。

这是我的最终代码:

// Send Request to Edge Server. Will receive a response as an Encoded stream
        String infoMessage = "userId Is: " + request.Info.UserId.AsString() + " Session Id is: " + request.Info.SessionId.AsString();
        String logMessage = "inside Invoke: " + infoMessage;
        Stream requestStream = null;
        HttpWebResponse webResponse = null;
        Stream responseStream = null;
         HttpWebRequest webRequest=null;
        try
        {
            // Construct web request
            logMessage = "Creating webrequest to: " + url + " " + infoMessage;
            webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.ContentType = encoderType.Trim().ToLower();
            webRequest.Method = REQUEST_METHOD;
            webRequest.Timeout = 120000;
            webRequest.ReadWriteTimeout = 120000;
            webRequest.KeepAlive = false;
            webRequest.Proxy = null;
            webRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
            webRequest.ServicePoint.MaxIdleTime = 5000;
            logMessage = logMessage + "Timeout is 5000 Keep Alive False";


            logMessage = "timout is: " + webRequest.Timeout.ToString() + logMessage;
            // Encode Request object as Stream
            requestStream = webRequest.GetRequestStream();
            EncoderFactory.GetInstance(this.encoderType).Encode(requestStream, request);
            logMessage = "going to getresponse" + logMessage;
            webResponse = (HttpWebResponse)webRequest.GetResponse();
            logMessage = "Response returend" + logMessage;
            // Recieve web response
            String contentType = webResponse.ContentType;
            logMessage = "going to get response stream" + logMessage;
            responseStream = webResponse.GetResponseStream();

            // If response Content Type is 'text/html' then assume it's an error message
            if (contentType.ToLower().Trim().StartsWith("text/html"))
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.Default);
                String errorMessage = reader.ReadToEnd();
                reader.Close();
                return new ErrorResponse(-1000, logMessage + " " + errorMessage);
            }
            else if (contentType.ToLower().Trim().StartsWith(EncoderFactory.ENCODER_TYPE_HESSIAN))
            {
                // Decode Response object from Stream
                return (Response)EncoderFactory.GetInstance(this.encoderType).Decode(responseStream);
            }
            else
            {
                return new ErrorResponse(-1001, "Invalid response - ContentType='" + contentType + "'");
            }
        }
        catch (Exception ex)
        {

            return new ErrorResponse(-1002, "Exception is -1002 and Message is = " + ex.Message + " " + logMessage + ".");
        }
        finally
        {

            // Close request stream
            if (requestStream != null)
            {
                try
                {
                    requestStream.Flush();
                    requestStream.Close();
                }
                catch { }
            }

            // Close web response
            if (webResponse != null)
            {
                try
                {
                    webResponse.Close();
                }
                catch { }
            }

            // Close response stream
            if (responseStream != null)
            {
                try
                {
                    responseStream.Flush();
                    responseStream.Close();
                }
                catch { }
            }
            GC.Collect();
        }
    }

0 个答案:

没有答案