System.Net.WebRequest - 超时错误

时间:2012-09-20 12:55:24

标签: c# asp.net http httpwebrequest webrequest

下面的代码正在给出 错误消息:“操作已超时” 错误Sourse:在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;
                }
            }
        }

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

2 个答案:

答案 0 :(得分:18)

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

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

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

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

事实上,如果您在整个过程中使用finally语句,则可以完全删除using块。

顺便说一下,这是一个很长的方法 - 77行! - 更糟糕的是,看起来它实际上是一个构造函数:

  • 尝试将其拆分为更小,更容易理解,更容易测试的块
  • 尽量避免在构造函数中做很多工作

答案 1 :(得分:2)

只是分享经验。

我收到同样的错误“操作超时”。

我尝试过使用WebClient和WebRequest(也设置了Timeout)但仍然出错。

原因是我没有处理回复。

所以我按照上面提到的那样使用了:

using (var response = oWebRequest.GetResponse())

{
    ...
}

它解决了这个问题...