我正在嘗試連接到 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。
我已經嘗試了許多代碼更改(您可以看到其中一些已被注釋掉),但到目前為止還沒有運氣。
uj5u.com熱心網友回復:
問題已解決。原因有很多:
- 我的代碼中 URL 的錯別字
- 供應商發布的 URL 不正確
- 當我應該首先從公司測驗服務器(服務器 IP 已列入白名單)部署它時,在本地執行 API 解決方案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/385690.html
