Ok. Here is the full code. I tried to reuse connections setting KeepAlive but it simply doesnt work. I looked in the Http messages using Feedler and Charles but all I can see is Connection: close in response.
I see 600 TCP connections in wait state opened by 10 threads. Each thread run one http requst at a time.
There is also bunch of responses which say - the unauthenticated request. The service requires digest authentication. The code obviously is static and simply run the same request few hundred times from different threads... So why some requestes fail to be authenticated?? I
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.C开发者_如何转开发redentials = 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);
}
If you are seeing Connection: Close in the response, does this not indicate to you that the server is forcing the connection to close after every request. I'm not sure there is anything you can do at this point to change that behaviour. Maybe you could ask the service provider why they are returning Connection: Close in every response.
I had a similar issue that went away after I set request.UnsafeAuthenticatedConnectionSharing = true and ServicePointManager.MaxServicePointIdleTime = 100000;//must not be 0
精彩评论