I cant make my Http connections to be reused. HttpWebRequest.KeepAlive setting makes no difference. I found this article which says that if you have NTLM authentication then use UnsafeAuthenticatedConnectionSharing = true.
So I set it to true and still have same bunch of TCP connections and nothing similar to reuse..
Any thoughts?
Sorry I dont know what pastebin is. Here is fragment of log I got doing 2 calls to the service. First call for some reason is going without authentication but it has Keep-Alive. The response is Unauthorized and Connection: Close. Then it resubmitted by system and but no Keep-Alive anymore and the response is OK but still Connection: Close. Unfortunately I cannot paste the whole log for security reasons.
System.Net Information: 0 : [5988] Associating HttpWebRequest#64923656 with ConnectStream#51393439
System.Net Information: 0 : [5988] HttpWebRequest#64923656 - Request: GET /v1.0/xxxxxx/xxxx/ HTTP/1.1
System.Net.Sockets Verbose: 0 : [5988] Socket#52727599::Send()
System.Net.Sockets Verbose: 0 : [5988] Data from Socket#52727599::Send
System.Net.Sockets Verbose: 0 : [5988] 00000000 : ...
System.Net.Sockets Verbose: 0 : [5988] Exiting Socket#52727599::Send() -> 103#103
System.Net Information: 0 : [5988] ConnectStream#51393439 - Sending headers
{
Host: xxxxxxxxxxxxxx
Connection: Keep-Alive
}.
System.Net.Sockets Verbose: 0 : [5988] Socket#52727599::Receive()
System.Net.Sockets Verbose: 0 : [5988] Data from Socket#52727599::Receive
System.Net.Sockets Verbose: 0 : [5988] 00000000 : 48 54 54 50 2F 31 2E 31-20 34 30 31 20 55 6E 61 : HTTP/1.1 401 Una
System.Net.Sockets Verbose: 0 : [5988] 00000010 : 75 74 68 6F 72 69 7A 65-64 0D 0A 44 61 74 65 3A : uthorized..Date:
System.Net.Sockets Verbose: 0 : [5988] 00000020 : 20 54 75 65 2C 20 30 36-20 4A 75 6C 20 32 30 31 : Tue, 06 Jul 201
System.Net.Sockets Verbose: 0 : [5988] 00000030 : 30 20 30 38 3A 34 34 3A-31 33 20 47 4D 54 0D 0A : 0 08:44:13 GMT..
System.Net.Sockets Verbose: 0 : [5988] 00000040 : 53 65 72 76 65 72 3A 20-41 70 61 63 68 65 2D 43 : Server: Apache-C
System.Net.Sockets Verbose: 0 : [5988] 00000050 : 6F 79 6F 74 65 2F 31 2E-31 0D 0A 57 57 57 2D 41 : oyote/1.1..WWW-A
System.Net.Sockets Verbose: 0 : [5988] 00000060 : 75 74 68 65 6E 74 69 63-61 74 65 3A 20 44 69 67 : uthenticate: Dig
System.Net.Sockets Verbose: 0 : [5988] 00000070 : 65 73 74 20 72 65 61 6C-6D 3D 22 xx xx xx xx xx : est realm="xxxxx
System.Net.Sockets Verbose: 0 : [5988] Exiting Socket#52727599::Receive() -> 689#689
System.Net Information: 0 : [5988] Connection#17654054 - Received status line: Version=1.1, StatusCode=401, StatusDescription=Unauthorized.
System.Net Information: 0 : [5988] Connection#17654054 - Received headers
{
Connection: close
Accept-Ranges: bytes
Content-Length: 312
Content-Type: text/html;charset=ISO-8859-1
Date: Tue, 06 Jul 2010 08:44:13 GMT
Server: Apache-Coyote/1.1
WWW-Authenticate: Digest realm="xxxxxxxxx", domain="null", qop="auth", algorithm=MD5, nonce=xxxxx
}.
System.Net Information: 0 : [5988] ConnectStream#26756241::ConnectStream(Buffered 312 bytes.)
System.Net Information: 0 : [5988] Associating HttpWebRequest#64923656 with ConnectStream#26756241
System.Net Information: 0 : [5988] Associating HttpWebRequest#64923656 with HttpWebResponse#23264094
System.Net Information: 0 : [5988] Enumerating security packages:
System.Net Information: 0 : [5988] Negotiate
System.Net Information: 0 : [5988] NegoExtender
System.Net Information: 0 : [5988] Kerberos
System.Net Information: 0 : [5988] NTLM
System.Net Information: 0 : [5988] Schannel
System.Net Information: 0 : [5988] Microsoft Unified Security Protocol Provider
System.Net Information: 0 : [5988] WDigest
System.Net Information: 0 : [5988] TSSSP
System.Net Information: 0 : [5988] pku2u
System.Net Information: 0 : [5988] LiveSSP
System.Net Information: 0 : [5988] CREDSSP
System.Net Warning: 0 : [5988] HttpWebRequest#64923656::() - Resubmitting request.
System.Net.Sockets Verbose: 0 : [5988] Socket#52727599::Dispose()
System.Net Information: 0 : [5988] Associating HttpWebRequest#64923656 with ServicePoint#44624228
System.Net Information: 0 : [5988] Associating Connection#14303791 with HttpWebRequest#64923656
System.Net.Sockets Verbose: 0 : [5988] Socket#49584532::Socket(InterNetwork#2)
System.Net.Sockets Verbose: 0 : [5988] Exiting Socket#49584532::Socket()
System.Net.Sockets Verbose: 0 : [5988] Socket#19699911::Socket(InterNetworkV6#23)
System.Net.Sockets Verbose: 0 : [5988] Exiting Socket#19699911::Socket()
System.Net.Sockets Verbose: 0 : [5988] Socket#49584532::Connect(218:18080#-628863380)
System.Net.Sockets Verbose: 0 : [5988] Exiting Socket#49584532::Connect()
System.Net.Sockets Verbose: 0 : [5988] Socket#19699911::Close()
System.Net.Sockets Verbose: 0 : [5988] Socket#19699911::Dispose()
System.Net.Sockets Verbose: 0 : [5988] Exiting Socket#19699911::Close()
System.Net Information: 0 : [5988] Associating HttpWebRequest#64923656 with ConnectStream#2389992
System.Net Information: 0 : [5988] HttpWebRequest#64923656 - Request: GET /v1.0/xxxxxxxxxxxxxxxx/ HTTP/1.1
System.Net.Sockets Verbose: 0 : [5988] Socket#49584532::Send()
System.Net.Sockets Verbose: 0 : [5988] Data from Socket#49584532::Send
System.Net.Sockets Verbose: 0 : [5988] 00000000 : 47 45 54 20 2F 76 31 2E-30 2F 64 65 61 6C 73 2F : GET /v1.0/xxxxx/...
...
System.Net.Sockets Verbose: 0 : [5988] Exiting Socket#49584532::Send() -> 394#394
System.Net Information: 0 : [5988] ConnectStream#2389992 - Sending headers
{
Authorization: Digest username="xxxx",realm="xxxxxxx",nonce=xxxxxxxx
Host: xxxxxxxxxxxxxxxxx
}.
System.Net.Sockets Verbose: 0 : [5988] Socket#49584532::Receive()
System.Net.Sockets Verbose: 0 : [5988] Data from Socket#49584532::Receive
System.Net.Sockets Verbose: 0 : [5988] 00000000 : 48 54 54 50 2F 31 2E 31-20 32 30 30 20 4F 4B 0D : HTTP/1.1 200 OK.
....
System.Net.Sockets Verbose: 0 : [5988] Exiting Socket#49584532::Receive() -> 337#337
System.Net Information: 0 : [5988] Connection#14303791 - Received status line: Version=1.1, StatusCode=200, StatusDescription=OK.
System.Net Information: 0 : [5988] Connection#14303791 - Received headers
{
Vary: Accept-Charset,Accept-Encoding,Accept-Language,Accept
Connection: close
Accept-Ranges: bytes
Content-Length: 87
Content-Type: text/plain;charset=ISO-8859-1
Date: Tue, 06 Jul 2010 08:44:13 GMT
Server: Apache-Coyote/1.1
}.
The test code:
HttpClient hc = new HttpClient();
hc.TransportSettings.Credentials = new NetworkCredential("xxxx", "xxxx");
hc.TransportSettings.ConnectionTimeout = TimeSpan.FromMilliseconds(timeout);
Uri url = new Uri(rest);
string data = string.Empty;
// 1
HttpResponseMessage resp = hc.Get(url);
data = (resp.StatusCode == HttpStatusCode.Forbidden || resp.StatusCode == HttpStatusCode.NotFound) ?
string.Empty : resp.Content.ReadAsString();
resp.Dispose();
// 2
resp = hc.Get(url);
data = (resp.StatusCode == HttpStatusCode.Forbidden || resp.StatusCode == HttpStatusCode.NotFound) ?
string.Empty : resp.Content.ReadAsString();