locked
http client untrust certificate error RRS feed

  • Question

  • Hi All,

    I am getting an error when I use http client and hit a https web service.

    I even installed the certificate but it shows the same error as httpclient certificate validation error

    But it works in on of my machine I don't know how but I thought certificate issues.

    Should I request for a new certificate from the client.

    Monday, January 5, 2015 12:37 PM

All replies

  • Is there any way I can fix the issue?

    code below:

    using

    (varhandler = GetHttpClientHandler())

                {

                   

    using(varclient = newHttpClient(handler))

                    {

                        client.DefaultRequestHeaders.Accept.Clear();

                        client.DefaultRequestHeaders.Add(

    "Accept", @"application/json;odata=verbose");

                        client.DefaultRequestHeaders.Add(

    "user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

                       

    HttpResponseMessageresponse = awaitclient.GetAsync(url);

                        response.EnsureSuccessStatusCode();

                       

    returnawaitresponse.Content.ReadAsStringAsync();

                    }

                }

    exception comes on response

    Tuesday, January 6, 2015 4:24 AM
  • this should fiix it:

     public async Task<string> Test(Uri url)
            {
                using (var handler = GetHttpClientHandler())
                {
                    using (var client = new HttpClient(handler))
                    {
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Add("Accept", @"application/json;odata=verbose");
                        client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
    
                        HttpResponseMessage response = await client.GetAsync(url);
                        response.EnsureSuccessStatusCode();
                        return await response.Content.ReadAsStringAsync();
                    }
    
                }
            }
    
            private Windows.Web.Http.Filters.HttpBaseProtocolFilter GetHttpClientHandler()
            {
                var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.BasicConstraintsError);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.IncompleteChain);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidCertificateAuthorityPolicy);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidSignature);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.OtherErrors);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationFailure);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationInformationMissing);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Revoked);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Success);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.UnknownCriticalExtension);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
                filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.WrongUsage);
                
                return filter;
            }

    but you havee to use the Windows.Web.Http.HttpClient and not the System.Net... version


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, January 6, 2015 8:44 AM