确定。这是完整的代码。我试图重用连接设置KeepAlive,但它根本不起作用。我使用Feedler和Charles查看了Http消息,但我能看到的只有Connection:关闭响应。
我看到有10个线程打开等待状态的600个TCP连接。每个线程一次运行一个http请求。
还有很多回复说 - 未经身份验证的请求。该服务需要摘要式身份验证。代码显然是静态的,只是从不同的线程运行相同的请求几百次...那么为什么有些请求无法进行身份验证? 我
static void GetRest(string rest)
{
int i = Interlocked.Increment(ref counter);
Uri uri = new Uri(rest);
CredentialCache cc = new CredentialCache();
cc.Add(uri, "Digest", new NetworkCredential("zz", "zz"));
ServicePointManager.FindServicePoint(uri).SetTcpKeepAlive(true, 6000000, 100000);
ServicePointManager.FindServicePoint(uri).ConnectionLimit = 5;
while (!stop)
{
HttpWebRequest req = WebRequest.Create(rest) as HttpWebRequest;
req.Credentials = cc;
req.Method = "GET";
req.Timeout = timeout;
req.KeepAlive = true;
try
{
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
StreamReader sr = new StreamReader(res.GetResponseStream());
string result = sr.ReadToEnd().Substring(0, 20);
int rc = Interlocked.Increment(ref responseCounter);
Console.Write(".");
Thread.Sleep(20);
}
}
catch (Exception ex)
{
Console.WriteLine("EXCEPTION {0}, {1}", i, ex.Message);
Interlocked.Increment(ref badResponseCounter);
}
}
Interlocked.Decrement(ref counter);
}
答案 0 :(得分:1)
如果您在响应中看到Connection:Close,这是否表明服务器在每次请求后都强制关闭连接。我不确定此时你能做些什么来改变这种行为。也许您可以询问服务提供商他们为什么要返回Connection:在每个响应中关闭。
答案 1 :(得分:0)
在我设置request.UnsafeAuthenticatedConnectionSharing = true后,我遇到了类似的问题。 ServicePointManager.MaxServicePointIdleTime = 100000; //不能为0