两次服务器500错误后,C#HttpWebRequest超时

时间:2010-10-27 12:30:56

标签: c# httpwebrequest

由于“(500)内部服务器错误500”而导致两个C#HttpWebRequests抛出异常后,第三次尝试抛出超时异常。为什么不抛出另一个(500)内部服务器错误异常?

当我重新启动应用程序时,它会抛出两个500错误,然后再次开始超时。

这是我的代码:

GetPages GetPages = new GetPages();
string test = GetPages.GetPage(); /* Exception: (500) Internal Server Error */
GetPages.Dispose();

GetPages GetPages = new GetPages();
string test = GetPages.GetPage();  /* Exception: (500) Internal Server Error */
GetPages.Dispose();

GetPages GetPages = new GetPages();
string test = GetPages.GetPage();  /* Exception: time out, why? */
GetPages.Dispose();

这是GetPages类和GetPage方法:

namespace MyNamespace
{
    class GetPages
    {
        public string GetPage()
        {
            this.httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl");

            try
            {
                StringBuilder postData = new StringBuilder(100);
                postData.Append("test=test");
                byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString());

                httpRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
                httpRequest.KeepAlive = false;
                httpRequest.Proxy = null;
                httpRequest.Method = "POST";
                httpRequest.Timeout = 10;
                httpRequest.ContentType = "application/x-www-form-urlencoded";

                httpRequest.ContentLength = dataArray.Length;

                using (this.requestStream = httpRequest.GetRequestStream())
                {
                    requestStream.Write(dataArray, 0, dataArray.Length);
                    requestStream.Flush();
                    requestStream.Close();

                    this.webResponse = (HttpWebResponse)httpRequest.GetResponse();

                    Stream responseStream = webResponse.GetResponseStream();
                    StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
                    String responseString = responseReader.ReadToEnd();


                    MessageBox.Show(responseString);
                    return responseString;
                }
            }

            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return "FAIL";
            }
        }

        public void Dispose()
        {
            System.GC.SuppressFinalize(this);
        }
    }
}

更新

谢谢大家的帮助。我无法解决这个问题。

处理方法现在已经消失。

我已经使用 HttpWebRequest httpRequest,HttpWebResponse webResponse和Stream requestStream local并使用以下使用语句:

using (webResponse = (HttpWebResponse)httpRequest.GetResponse())
{
    using (Stream responseStream = webResponse.GetResponseStream())
    {
        using (StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8))
        {
            responseString = responseReader.ReadToEnd();
        }
    }
}

另一次更新

现在这是整个GetPage方法:

public string GetPage()
{
    HttpWebRequest httpRequest;
    HttpWebResponse webResponse;
    Stream requestStream;

    try
    {
        StringBuilder postData = new StringBuilder(100);
        postData.Append("test=test");
        byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString());

        httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl");

        httpRequest.Proxy = null;
        httpRequest.Method = "POST";
        httpRequest.Timeout = 10;
        httpRequest.ContentType = "application/x-www-form-urlencoded";

        httpRequest.ContentLength = dataArray.Length;

        using (requestStream = httpRequest.GetRequestStream())
        {
            /* this is never reached when the time out exception starts 
            so the error seems to be related to too many GetRequestStreams */

            requestStream.Write(dataArray, 0, dataArray.Length);

            webResponse = (HttpWebResponse)httpRequest.GetResponse();

            /* this is never reached when the 500 server error occurs */

            String responseString = "";
            using (Stream responseStream = webResponse.GetResponseStream())
            {
                using (StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8))
                {
                    responseString = responseReader.ReadToEnd();
                    return responseString;
                }
            }
        }
    }

    catch (Exception e)
    {
        return "FAIL";
    }

    return "...";
}

**已解决!! ** httpRequest没有得到中止()。在catch()块中,我添加了httpRequest.abort(),现在它可以正常工作。

2 个答案:

答案 0 :(得分:12)

怀疑这就是问题:

this.webResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8);
String responseString = responseReader.ReadToEnd();

您没有丢弃任何这些一次性物品。这意味着将保持与Web服务器的连接直到完成,因此连接池(每个服务器有两个连接)将不允许任何其他请求。

我建议:

  • 您将GetResponse调用移出请求流的using语句
  • 您删除了对FlushClose的请求流的显式调用(处理它已经足够了)
  • 你制作webResponsewebRequest局部变量而不是实例变量,除非你以后 需要它们,在这种情况下你应该在{{1}中处理它们方法。
  • 您对DisposeusingWebResponse使用Stream语句。 (最后一个并不是绝对必要的,因为处理流足够了。)
  • 除非您真的需要,否则StreamReader不会实施GetPages

答案 1 :(得分:7)

HTTP协议定义只能同时向同一服务器建立两个连接。成功或不成功阅读后关闭responseStream