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.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);
}