底层连接已关闭 - API端点调用失败

问题描述:

我试图确定为什么在使用HTTPS而不是来自Windows Server 2012 R2的HTTP时,对Restful API端点的调用失败。当我在我的笔记本电脑上本地运行应用程序时(Windows 7),同样的HTTPS调用起作用。底层连接已关闭 - API端点调用失败

不幸的是,我不能让提琴手显示从Web服务器上的应用程序到API的调用。 试图枚举AppContainers列表的AppContainer Loopback Exemption Utility错误。服务器也被组策略锁定,所以我无法关闭防火墙来尝试解决提琴手问题。

到API的调用是:收到

public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request) 
{ 
    client = new HttpClient(); 

    // removed code for setting ignoreCertErrors and environmentName 

    if (ignoreCertErrors && environmentName.ToUpper() != "PROD") 
      { 
       ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 
      } 

    return client.SendAsync(request); 
} 

错误:

================================================================= Message: The underlying connection was closed: An unexpected error occurred on a send. Stack Trace:
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResultar)

================================================================= Message: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. Stack Trace:
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)

================================================================= Message: An existing connection was forcibly closed by the remote host Stack Trace:
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)

=================================================================

如何获得更多的细节?我试图避免像Wireshark这样的低级工具,我希望有选项跟踪/日志/捕获有关API失败的应用程序调用的更多细节。

更新 - 这里是追踪信息时失败:

System.Net Information: 0 : [2292] SecureChannel#49584532::.ctor(hostname=XX.XX.com, clientCertificates=0, encryptionPolicy=RequireEncryption)

System.Net Information: 0 : [2292] SecureChannel#49584532 - Left with 0 client certificates to choose from.

System.Net Information: 0 : [2292] Using the cached credential handle. System.Net Information: 0 : [2292] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = XX.XX.com, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation) System.Net Information: 0 : [2292] InitializeSecurityContext(In-Buffer length=0, Out-Buffer length=178, returned code=ContinueNeeded).

System.Net.Sockets Verbose: 0 : [2292] Socket#14347911::BeginSend()

System.Net.Sockets Verbose: 0 : [2292] Exiting Socket#14347911::BeginSend() -> OverlappedAsyncResult#19699911 System.Net.Sockets Verbose: 0 : [2292] Data from Socket#14347911::PostCompletion

[ Removed ]

System.Net.Sockets Verbose: 0 : [2292] Socket#14347911::EndSend(OverlappedAsyncResult#19699911)

System.Net.Sockets Verbose: 0 : [2292] Exiting Socket#14347911::EndSend() -> Int32#178

System.Net.Sockets Verbose: 0 : [2292] Socket#14347911::BeginReceive()

System.Net.Sockets Verbose: 0 : [2292] Exiting Socket#14347911::BeginReceive() -> OverlappedAsyncResult#2389992

System.Net.Sockets Verbose: 0 : [2292] Socket#14347911::EndReceive(OverlappedAsyncResult#2389992)

System.Net.Sockets Error: 0 : [2292] Socket#14347911::UpdateStatusAfterSocketError() - ConnectionReset

System.Net.Sockets Error: 0 : [2292] Exception in Socket#14347911::EndReceive - An existing connection was forcibly closed by the remote host. System.Net.Sockets Verbose: 0 : [2292] Exiting Socket#14347911::EndReceive() -> Int32#0

System.Net.Sockets Verbose: 0 : [2292] Socket#14

+0

对api的调用可能失败。当您连接到服务器时,您使用的访客权限通常不具有在服务器上运行api的凭据。必须将api安装在服务器上才能以管理员权限运行。为了验证我是正确的,我将检查服务器上的事件查看器以获取有关该错误的更多详细信息。 – jdweng

+0

对使用​​HTTP的API的调用起作用,但是当我切换API URI时使用HTTPS会引发上述错误。 – Josh

+0

你测试空响应吗?不确定的根本原因,但我怀疑你可能会得到空包。 – jdweng

的日志条目:

System.Net.Sockets Error: 0 : [2292] Socket#14347911::UpdateStatusAfterSocketError() - ConnectionReset

被认为是阻塞端口防火墙引起的。一旦添加新的防火墙规则,问题就解决了。