5

以下代码给出 错误消息:“操作已超时”错误源:在 System.Net.httpWebRequest.GetResponse()

此方法正在调用 URL 并获取响应对象。

注意:这一切在我最后都可以正常工作..但是当我将相同的代码发送到生产环境时..它显示超时错误

public GetUpdatedInventoryUnitValues(Vehicle aeVehicle)
{
            WebRequest oWebRequest = null;
            StringBuilder oStringBuilder = null;
            StreamReader oStreamReader = null;
            dcDealerDetails = new Dictionary<string, string>();

            MSRP = string.Empty;
            NetPrice = string.Empty;
            string strLine = string.Empty;
            string strURL = GetUpdatedInventoryUnitValues.GetFormattedURL(aeVehicle);

            try
            {
                /* Open the requested URL */
                oWebRequest = WebRequest.Create(strURL);
                oWebRequest.Method = "GET";
                oWebRequest.ContentType = "application/xml";
                /* Get the stream from the returned web response */
                oStreamReader = new StreamReader(oWebRequest.GetResponse().GetResponseStream());
                /* Get the stream from the returned web response */
                oStringBuilder = new StringBuilder();
                /* Read the stream a line at a time and place each one into the stringbuilder  */
                while ((strLine = oStreamReader.ReadLine()) != null)
                {
                    /* Ignore blank lines */
                    if (strLine.Length > 0)
                        oStringBuilder.Append(strLine);
                }

                string[] tempArray = null;
                string[] tempNextArray = null;
                //Split string by semicolon as a separater
                tempArray = Data.SplitString(oStringBuilder.ToString(), new char[] { ';' });

                if (tempArray != null)
                {
                    foreach (string invUnits in tempArray)
                    {
                        //Split string by '=' as a separater
                        tempNextArray = Data.SplitString(invUnits, new char[] { '=' });

                        if (tempNextArray != null && tempNextArray.Length == 2)
                        {
                            switch (tempNextArray[0].ToLower())
                            {
                                //case "msrp":
                                //    MSRP = Data.RemoveDoubleCode(tempNextArray[1]);
                                //    break;
                                case "netprice":
                                    NetPrice = Data.RemoveDoubleCode(tempNextArray[1]);
                                    break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLog.ErrorMessage = ErrorLog.Separator;
                ErrorLog.ErrorMessage = "Exception during posting data to another application .";
                ErrorLog.ErrorMessage = "ERROR MESSAGE : " + ex.Message;
                ErrorLog.ErrorMessage = "ERROR SOURCE: " + ex.StackTrace.ToString();

            }
            finally
            {
                if (oStreamReader != null)
                {
                    oStreamReader.Close();
                }
                if (oWebRequest != null)
                {
                    oWebRequest = null;
                }
            }
        }

请建议我做错了什么或错过了什么?

4

3 回答 3

18

您是否发现前几个请求没问题,然后它们开始超时?如果是这样,我怀疑这是问题所在:

oStreamReader = new StreamReader(oWebRequest.GetResponse().GetResponseStream());

您正在获取响应,但从不处理它。你应该使用:

using (var response = oWebRequest.GetResponse())
{
    ...
}

事实上,如果您始终使用语句,您可以完全摆脱finally阻塞。using

As an aside, this is a pretty long method - 77 lines! - and worse, it looks like it's actually a constructor:

  • Try to split it out into smaller, more easily understood, more easily testable chunks
  • Try to avoid doing a lot of work in constructors
于 2012-09-20T12:59:54.243 回答
2

Just sharing experience.

I was getting the same error "Operation Timed Out".

I have tried using both WebClient And WebRequest (set Timeout also) but was still getting error.

Reason was that I was not disposing the response.

So I used as mentioned above :

using (var response = oWebRequest.GetResponse())

{
    ...
}

And it solved the problem...

于 2013-04-04T09:13:47.403 回答
1

I personnally use this code for one of my program and it works perfect:

    WebRequest webRequest = WebRequest.Create(requestUri);
    webRequest.Credentials = new NetworkCredential(login, password);
    WebResponse webResponse = webRequest.GetResponse();
    Stream response = webResponse.GetResponseStream();
    StreamReader reader = new StreamReader(response);

So i think it doesn't come from your code but from your production platform.

于 2012-09-20T13:05:55.260 回答