次の関数をスレッド化していますが、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++;
}