0

我正在线程化以下函数,当我运行超过 10 个线程时,我开始得到一堆超时连接..

我已添加_wReq.ServicePoint.ConnectionLimit = 100;到方法中,并将以下内容添加到我的 app.config

<system.net>
    <connectionManagement>
        <add address = "*" maxconnection = "200" />
    </connectionManagement>
</system.net>

这是方法:

public string websocket(string proxy, string URL)
{
    string _html = "";
    HttpWebResponse _wResp = null;

    try
    {
        HttpWebRequest _wReq;
        System.Text.ASCIIEncoding _enc = new System.Text.ASCIIEncoding();
        _wReq = (HttpWebRequest)HttpWebRequest.Create(URL);
        _wReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
        _wReq.Accept = "*/*";
        _wReq.Method = "GET";
        _wReq.CookieContainer = cookieJarf;
        _wReq.ServicePoint.ConnectionLimit = 100;
        _wReq.KeepAlive = false;
        _wReq.Timeout = 5000;
        _wReq.ReadWriteTimeout = 5000;
        _wReq.Pipelined = true;

        if (proxy != "")
        {
            WebProxy myproxy = new WebProxy(proxy);
            _wReq.Proxy = myproxy;
        }

        _wResp = (HttpWebResponse)_wReq.GetResponse();
        Encoding responseEncoding = Encoding.GetEncoding(_wResp.CharacterSet);
        using (StreamReader sr = new StreamReader(_wResp.GetResponseStream(), responseEncoding))
        {
            _html = sr.ReadToEnd();
        }

        cookieJarf = _wReq.CookieContainer;
        _wResp.Close();
    }
    catch (WebException wexc1)
    {
        MessageBox.Show(wexc1.Message); // Time out exception when running more than 10 threads

        if (wexc1.Status == WebExceptionStatus.ProtocolError)
        {
            return "";
        }
    }
    finally
    {
         if (_wResp != null)
             _wResp.Close();
    }
    return _html;
}

我不知道我应该尝试什么:/ 当然它必须可以同时运行例如 50 个 httpwebrequests?

编辑:应该说,这些线程连接中的一些连接到同一台服务器,而另一些连接到不同的服务器。我基本上将这种方法用于我的线程爬虫。

编辑1:

单击按钮后,我将像这样添加我的线程:

foreach (string item in urlQueue)
{
    _smartThreadPool.QueueWorkItem(
    new Amib.Threading.Func<string, int, int, string, int>(checkURL),
    item, iia, 5000, kryptonTextBox1.Text);

    iia++;
}
4

0 回答 0