【问题标题】:Why is my API method returing Received an unexpected EOF or 0 bytes from the transport stream为什么我的 API 方法返回从传输流中接收到意外的 EOF 或 0 字节
【发布时间】:2021-12-13 23:55:22
【问题描述】:

我正在尝试连接到 API。该 API 旨在将 XML 作为 POST 请求发送到接收 API。我正在使用下面的代码来尝试实现这一点。

    [HttpPost]
    [Route("PostCadXmlApiKey")]
    public async Task<string> PostCadXmlApiKey()
    {
        using (var httpClient = new HttpClient())

        {
            try
            {
                CadDto cadDto = new CadDto();

                cadDto = await _cadService.GetCadXmlFromRepoAsync();

                string carmUrl = _config.GetValue<string>("Lii:Values:CarmUrl");
                string carmKey = _config.GetValue<string>("Lii:Values:CarmKey");

                var content = new StringContent(cadDto.CadXml, Encoding.UTF8, "application/xml");

                //HttpClientHandler clientHandler = new HttpClientHandler();
                //clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };

                //ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) =>
                //{ return true; };
                //ServicePointManager.DefaultConnectionLimit = 9999;
                //ServicePointManager.Expect100Continue = true;
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;

                httpClient.BaseAddress = new Uri(carmUrl);
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Add("x-API-KEY", carmKey);
                httpClient.Timeout= TimeSpan.FromSeconds(100); 

                HttpResponseMessage response = await httpClient.PostAsync("commercialAccoutingDeclarations", content);

                httpClient.DefaultRequestHeaders.ConnectionClose = true;

                if (response.StatusCode == HttpStatusCode.OK)
                { 
                    string result = await response.Content.ReadAsStringAsync();
                    if (string.IsNullOrEmpty(result))
                        return "Success";
                    else
                        return result;
                }
                else if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new UnauthorizedAccessException();
                }
                else
                {
                    throw new Exception(await response.Content.ReadAsStringAsync());
                }
            }
            catch(Exception ex)
            {
                string message = ex.Message;
            }
        }

        return null;
    }

当我执行此操作时,我收到以下错误:

无法建立 SSL 连接,请参阅内部异常。

内部异常是:从传输流中接收到意外的 EOF 或 0 字节。

这是一个 .net 6.0 C# Web Api。

我已经尝试了许多代码更改(其中一些您可以看到已被注释掉),但到目前为止都没有运气。

【问题讨论】:

标签: c# web webapi


【解决方案1】:

问题已解决。原因有很多:

  • 我的代码中的 URL 拼写错误
  • 供应商发布的 URL 不正确
  • 在我应该首先部署 API 解决方案时在本地执行它 来自公司测试服务器(服务器 IP 已列入白名单)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2022-12-16
    • 2016-06-07
    • 1970-01-01
    相关资源
    最近更新 更多