在.NET中使用HttpWebResponse超时

时间:2010-10-28 07:03:00

标签: .net httpwebrequest httpwebresponse

我有以下代码,在调用它约60次后(20个并发连接),它开始超时。如果我将超时时间从10分钟降低到1分钟,他们会在~34下载时开始计时。是什么赋予了?我知道如果你没有正确地关闭你的回复,你可以得到这个,但我肯定会关闭它:

    //===============================================================================
    /// <summary>
    /// Executes the request and returns the response as a byte array. Useful if the 
    /// response should return a file.
    /// </summary>
    private static byte[] GetResponse(HttpWebRequest webRequest)
    {
        //---- declare vars
        HttpWebResponse response = null;
        List<byte> buffer = new List<byte>();
        int readByte;

        //---- try to get the response, always wrap it.
        try
        { response = webRequest.GetResponse() as HttpWebResponse; }
        //---- catch all
        catch (Exception e)
        {
            if (response != null) { response.Close(); }
            throw new ConnectionFailedException("Failed to get a response", e);
        }

        try
        {
            //---- if the response is ok
            if (response.StatusCode == HttpStatusCode.OK)
            {
                //---- get the response stream
                using (Stream stream = response.GetResponseStream())
                {
                    //---- read each byte, one by one into the byte buffer
                    while ((readByte = stream.ReadByte()) > -1)
                    {
                        buffer.Add((byte)readByte);
                    }
                    //---- close the stream
                    stream.Close();
                    response.Close();
                }

                //---- return the buffer as a byte array
                return buffer.ToArray();
            }
            //---- if the request wasn't auth'd
            else if (response.StatusCode == HttpStatusCode.Forbidden || response.StatusCode == HttpStatusCode.Unauthorized)
            {
                if (response != null) { response.Close(); }
                throw new AuthenticationFailedException(response.StatusDescription);
            }
            //---- any other errors
            else
            {
                if (response != null) { response.Close(); }
                throw new ConnectionFailedException(response.StatusDescription);
            }
        }
        finally { if (response != null) { response.Close(); } }
    }
    //===============================================================================

想法?

另外,我正在创建它,TimeOut和ReadWriteTimeout都设置为10分钟:

// ----创建Web请求 HttpWebRequest webRequest = WebRequest.Create(url)as HttpWebRequest;

// ----设置10分钟超时 webRequest.Timeout = 600000; webRequest.ReadWriteTimeout = 600000;

4 个答案:

答案 0 :(得分:4)

System.Net.ServicePointManager.DefaultConnectionLimit = 200;

^^ 完成。

就是这样。

答案 1 :(得分:1)

如何简化代码:

using (var client = new WebClient())
{
    byte[] result = client.DownloadData("http://example.com");
}

答案 2 :(得分:0)

通过以下方式将KeepAlive属性设置为false:

webRequest.KeepAlive = false;

在finally语句中释放资源。

答案 3 :(得分:0)

未经测试,但有点清洁。

private static byte[] GetResponse(HttpWebRequest webRequest)
{
        using (var response = (HttpWebResponse)webRequest.GetResponse())
        {
            switch (response.StatusCode)
            {
                case HttpStatusCode.Forbidden:
                case HttpStatusCode.Unauthorized:
                    throw new AuthenticationFailedException(response.StatusDescription);
                    break;
                case HttpStatusCode.OK:
                    break; // to get through
                default:
                    throw new ConnectionFailedException(response.StatusDescription);
            }

            using (Stream stream = response.GetResponseStream())
            {
                // you should really create a large buffer and read chunks.
                var buffer = new byte[response.ContentLength];
                var bytesRead = 0;
                while (bytesRead < buffer.Length)
                {
                   var bytes = stream.Read(buffer, bytesRead, buffer.Length - bytesRead);
                   bytesRead += bytes;
                }

                return buffer;
            }

        }
}

修改

已更改,以便缓冲区分配使用ContentLength。除非使用分块编码,否则它总是精确的。