2

I am attempting to connect to an API. The API is designed to send XML to the receiving API as a POST request.I am using the below code to try and facilitate this.

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

When I execute this then I receive the following error :

The SSL connection could not be established, see inner exception.

The inner exception is : Received an unexpected EOF or 0 bytes from the transport stream.

This is a .net 6.0 C# Web Api.

I've tried a number of code changes (some of which you can see commented out) but so far no luck.

Rez Khan
  • 43
  • 1
  • 5
  • Does this answer your question? [Getting EOF exception over https call](https://stackoverflow.com/questions/5382548/getting-eof-exception-over-https-call) – Hanlet Escaño Dec 13 '21 at 23:57

1 Answers1

1

The issue has been resolved. It was due to a number of things:

  • Typo of URL in my code
  • Incorrect URL published by vendor
  • Executing the API solution locally when I should have deployed it first from the companies test server (the server IP was whitelisted).
ZygD
  • 22,092
  • 39
  • 79
  • 102
Rez Khan
  • 43
  • 1
  • 5