RemoteCertificateNameMismatch, RemoteCertificateChainErrors with HttpClient

Kmcnet 691 Reputation points
2023-02-04T23:37:12.8466667+00:00

Hello everyone and thanks for the help in advance. I am refactoring code that previously used WebClient (now deprecated) to HttpClient. The page connects with to a device using a self-signed certificate with an internal IP address for the Uri. Therefore the device is trusted. Previously, using WebClient, I used the following:



However, 


                using (var client = new HttpClient(handler))
                {
                    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                    byte[] fileBytes = await client.GetByteArrayAsync(uri);
                    File.WriteAllBytes(CSVPath, fileBytes);
                }

throws RemoteCertificateNameMismatch, RemoteCertificateChainErrors
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,237 questions
{count} votes

Accepted answer
  1. QiYou-MSFT 4,306 Reputation points Microsoft Vendor
    2023-02-07T09:04:02.51+00:00

    Hi @Kmcnet,

    First, I looked up the documentation at Microsoft Docs and tested the examples.

    Then I looked at the information of the relevant code, and there are no variables involved here. So I brought your code straight in for testing.

    Picture1

    I found that your code can't read the information in the URL.

    Picture2

    I think you can modify the code a bit:

    HttpClientHandler handler = new HttpClientHandler { Credentials = new NetworkCredential(UserName, Password) };
    handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
    using (var client = new HttpClient(handler))
                    {
                        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                        byte[] fileBytes = await client.GetByteArrayAsync(uri);
                        File.WriteAllBytes(CSVPath, fileBytes);
                    }
    

    change to

    HttpClientHandler handler = new HttpClientHandler { Credentials = new NetworkCredential(UserName, Password) };
    handler.ServerCertificateCustomValidationCallback = ServerCertificateCustomValidation;
    using (var client = new HttpClient(handler))
                    {
                       
                        byte[] fileBytes = await client.GetByteArrayAsync(uri);
                        File.WriteAllBytes(CSVPath, fileBytes);
                    }
    

    For more you can check out the documentation:

    Document

    Of course, this could also be an issue with the URL.

    Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Kmcnet 691 Reputation points
    2023-02-06T22:45:39.1966667+00:00

    You need to declare a handler, then add the certificate override to the handler.

    HttpClientHandler handler = new HttpClientHandler { Credentials = new NetworkCredential(UserName, Password) };
    handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
    
    
    0 comments No comments

  2. Kmcnet 691 Reputation points
    2023-02-06T22:47:05.3+00:00

    You need to declare a handler, then add the certificate override to the handler:

    handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
    
    
    0 comments No comments