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

问题描述 投票:1回答:1

我有以下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管理后面的dev Azure环境中,操作方法/控制器如下所示:

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

以下是Azure API Management中的协议设置:Protocols

为什么我在Powershell中获得SocketException但在C#中没有,我怎样才能在Powershell中使用它?

azure powershell http asp.net-core azure-api-management
1个回答
1
投票

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

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

我相信这是对正在使用的.NET版本的限制。 .NET 4.6以下的任何版本都将默认使用非1.2的TLS版本。

© www.soinside.com 2019 - 2024. All rights reserved.