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

The underlying connection was closed -- API endpoint call fails

我正在尝试确定为什么当我使用 HTTPS 而不是来自 Windows Server 2012 R2 的 HTTP 时,对 Restful API 端点的调用失败。当我 运行 在我的笔记本电脑 (Windows 7) 上本地应用程序时,相同的 HTTPS 调用有效。

不幸的是,我无法让 Fiddler 显示 Web 服务器上的应用程序对 API 的调用。 AppContainer Loopback Exemption Utility 尝试枚举 AppContainers 列表时出错。服务器也被组策略锁定,因此我无法关闭防火墙来尝试解决 Fiddler 问题。

对 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,我希望有选项 trace/log/capture 有关应用程序调用失败的 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

日志条目:

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

是由阻止端口的防火墙引起的。添加新的防火墙规则后,问题就解决了。