为什么我的 API 方法返回 Received an unexpected EOF or 0 bytes from the transport stream
Why is my API method returing Received an unexpected EOF or 0 bytes from the transport stream
我正在尝试连接 API。 API 旨在将 XML 发送到接收方 API 作为 POST request.I 我正在使用以下代码来尝试和促进这一点。
[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# 网站 Api。
我已经尝试了一些代码更改(您可以看到其中一些已被注释掉)但到目前为止没有成功。
问题已解决。这是由于很多事情:
- 我的代码中 URL 错别字
- 供应商发布的 URL 不正确
- 在我本应首先部署的情况下在本地执行 API 解决方案
来自公司测试服务器(服务器 IP 已列入白名单)。
我正在尝试连接 API。 API 旨在将 XML 发送到接收方 API 作为 POST request.I 我正在使用以下代码来尝试和促进这一点。
[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# 网站 Api。
我已经尝试了一些代码更改(您可以看到其中一些已被注释掉)但到目前为止没有成功。
问题已解决。这是由于很多事情:
- 我的代码中 URL 错别字
- 供应商发布的 URL 不正确
- 在我本应首先部署的情况下在本地执行 API 解决方案 来自公司测试服务器(服务器 IP 已列入白名单)。