代码之家  ›  专栏  ›  技术社区  ›  David Klempfner

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

  •  0
  • David Klempfner  · 技术社区  · 6 年前

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

    Protocols

    为什么我会得到 SocketException 在Powershell中,而不是在C#中,如何让它在Powershell中工作?

    0 回复  |  直到 6 年前
        1
  •  1
  •   AdminOfThings    6 年前

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

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

    我认为这是使用.NET版本的一个限制。任何低于.NET 4.6的版本都将默认使用不是1.2的TLS版本。