C#如何强制重用经过身份验证的Http连接?

C#如何强制重用经过身份验证的Http连接?,c#,http,rest,web-services,httpwebrequest,C#,Http,Rest,Web Services,Httpwebrequest,我无法重复使用我的Http连接。HttpWebRequest.KeepAlive设置没有区别。我发现这篇文章说,如果您有NTLM身份验证,那么就使用unsafeauthenticatedConnectionShareg=true 所以我将其设置为true,并且仍然有相同的TCP连接,与重用没有任何相似之处 有什么想法吗 对不起,我不知道pastebin是什么。这是我对服务进行了2次调用的日志片段。由于某种原因,第一个调用在没有身份验证的情况下进行,但它保持活动状态。响应未经授权,连接:关闭。然

我无法重复使用我的Http连接。HttpWebRequest.KeepAlive设置没有区别。我发现这篇文章说,如果您有NTLM身份验证,那么就使用unsafeauthenticatedConnectionShareg=true

所以我将其设置为true,并且仍然有相同的TCP连接,与重用没有任何相似之处

有什么想法吗


对不起,我不知道pastebin是什么。这是我对服务进行了2次调用的日志片段。由于某种原因,第一个调用在没有身份验证的情况下进行,但它保持活动状态。响应未经授权,连接:关闭。然后由系统重新提交,但不再保持活动状态,响应正常,但仍然连接:关闭。不幸的是,出于安全原因,我无法粘贴整个日志

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

测试代码:

        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();