gpt4 book ai didi

azure - 转换为 Powershell 的工作 C# 代码会导致 SocketException

转载 作者:可可西里 更新时间:2023-11-01 17:17:16 26 4
gpt4 key购买 nike

我有以下 C# 控制台应用程序,它成功调用 HTTP Post 端点:

class Program
{
static void Main(string[] args)
{
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://DomainOne.azure-api.net");
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8b17badbbfe44e9e933453a8b4d8fc85");

StringContent stringContent = new StringContent("\"hi\"", System.Text.Encoding.UTF8, "application/json");
Task<HttpResponseMessage> task = httpClient.PostAsync("integration/api/Result/CompanyOne", stringContent);
HttpResponseMessage response = task.Result;

var content = response.Content.ReadAsStringAsync().Result;
}
}

这是我尝试使用的等效 Powershell 脚本:

$httpClient = New-Object 'System.Net.Http.HttpClient'
$httpClient.BaseAddress = New-Object 'System.Uri'('https://DomainOne.azure-api.net')
$httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8b17badbbfe44e9e933453a8b4d8fc85");

$stringContent = New-Object 'System.Net.Http.StringContent'('"hi"', [System.Text.Encoding]::UTF8, 'application/json')
$task = $httpClient.PostAsync('integration/api/Result/CompanyOne', $stringContent)
$response = $task.Result

$content = $response.Content.ReadAsStringAsync().Result

但是 $task 对象包含以下内容:

System.AggregateException: One or more errors occurred. 
---> System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.
---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---

端点托管在 API 管理后面的开发 Azure 环境中,操作方法/ Controller 如下所示:

[ApiController]
[Route("api/[controller]")]
public class ResultController : ControllerBase
{
[Route("{providerName}")]
[HttpPost]
public Task<bool> ReceiveMessage(HL7Message message)
{
return Task.FromResult(true);
}
}

以下是 Azure API 管理中的协议(protocol)设置: Protocols

为什么我在 Powershell 中得到 SocketException 而在 C# 中却没有,如何让它在 Powershell 中工作?

最佳答案

基于Rick Rainey's helpful comment ,您可以通过在 HTTP 调用之前添加以下代码行来强制使用 TLS 版本:

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

我认为这是所使用的 .NET 版本的限制。 .NET 4.6 以下的任何版本都将默认使用 1.2 以外的 TLS 版本。

关于azure - 转换为 Powershell 的工作 C# 代码会导致 SocketException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com