NET中的HttpWebResponse超时 [英] Timeout with HttpWebResponse in .NET

查看:137
本文介绍了NET中的HttpWebResponse超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,在调用它约60次(20个并发连接)之后,它开始超时.如果我将超时时间从10分钟降低到1分钟,那么它们将在〜34次下载时开始超时.是什么赋予了?我知道,如果您没有正确关闭响应,就可以得到它,但是我肯定会关闭它:

I have the following code, and after ~60 times calling it (20 concurrent connections) it starts timing out. if i lower the timeout from 10 minutes to 1 minute, they start timing out at ~34 downloads. what gives? i know that you can get this if you don't properly close your response, but i'm definitely closing it:

    //===============================================================================
    /// <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分钟来创建它:

also, i'm creating it with both the TimeOut and ReadWriteTimeout set to 10 minutes:

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

//---- create the web request HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;

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

//---- set a 10 minute timeout webRequest.Timeout = 600000; webRequest.ReadWriteTimeout = 600000;

推荐答案

System.Net.ServicePointManager.DefaultConnectionLimit = 200;

System.Net.ServicePointManager.DefaultConnectionLimit = 200;

^^ 完成.

就是这样.

这篇关于NET中的HttpWebResponse超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆