views:

51

answers:

2

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);
    }
+1  A: 

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.

Darrel Miller
I would ask. But I am newbie in all those HTTP things.. Are you sure that this is server job? I dont see Keep-Alive header in my requests either... May be it reponse with Close because my client code doesnt ask for it to be kept alive. Look here: http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/87bc7029-ce23-438a-a767-f7c32dcc63a7 *authenticated requests* will not be reused! I tried UseunsafeAuthenticatedConnectionSharing = true but it didnt work. the thread is from 2005.
Bobb
A: 

Can you reduce your application down to just two requests sent sequentially, and then create a system.net tracelog and put it on pastebin so that we can see?

Whether a connection gets reused or not depends on the client, server and the protocol being used.

For HTTP/1.0, connection is not reused by default, unless the client/server agree to keep the connection alive by using the "Connection: keep-alive" header.

For HTTP/1.1 protocol, connection is keep-alive by default, unless client/sever want to close connection by specifying the "connection: close" header.

This is complicated by behavior specific to authentication protocols implemented by different clients. For eg, for NTLM auth, in order to be secure by default, no connection reuse is done (remember NTLM is a connection authentication mechanism, not a request authentication mechanism). For NTLM you can force connection reuse by specifying "UnsafeAuthenticationConnectionSharing=true".

Anyway, please put the log on pastebin and we can take a look.

feroze
Thank you.. This question evloved into this one: http://stackoverflow.com/questions/3182689/c-how-to-force-reusing-authenticated-http-connections as I think is the authentication makes it close connections all the time
Bobb
I tried pure HttpWebRequest with UnsafeAuthenticationConnectionSharing=true but it didnt make any difference.
Bobb