Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8419797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:42:46+00:00 2026-06-10T02:42:46+00:00

Apache Http Client. You can see the relevant code here : String url =

  • 0

Apache Http Client. You can see the relevant code here:

String url = "https://path/to/url/service";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);

// Test whether to ignore cert errors
if (ignoreCertErrors){
  TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager(){
      public X509Certificate[] getAcceptedIssuers(){ return null; }
      public void checkClientTrusted(X509Certificate[] certs, String authType) {}
      public void checkServerTrusted(X509Certificate[] certs, String authType) {}
    }
  };

  try {
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
  } catch (Exception e){
    e.printStackTrace();
  }
}

try {

  // Execute the method (Post) and set the results to the responseBodyAsString()
  int statusCode = client.executeMethod(method);
  resultsBody = method.getResponseBodyAsString();

} catch (HttpException e){
  e.printStackTrace();
} catch (IOException e){
  e.printStackTrace();
} finally {
  method.releaseConnection();
}

This is the method everyone says to use to ignore SSL Certificate Errors (only setting this up for staging, it won’t be used in production). However, I am still getting the following exception/stacktrace:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building unable to find valid certification path to requested target

Any tips would be great. If I am doing the TrustManager wrong, or if I should be executing the HTTP Post method differently, either way.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:42:47+00:00Added an answer on June 10, 2026 at 2:42 am

    First, don’t ignore certificate errors. Deal with them instead. Ignoring certificate errors opens the connection to potential MITM attacks. It’s like turning off the buzzer in your smoke alarm because sometimes it makes a noise…

    Sure, it’s tempting to say it’s only for test code, it won’t end up in production, but we all know what happens when the deadline approaches: the code doesn’t show any error when it’s being tested -> we can ship it as it is. You should set up a test CA instead if you need. It’s not very hard to make, and the overall process is certainly no harder than introducing custom code for development and removing it in production.

    You’re visibly using Apache Http Client:

    HttpClient client = new HttpClient();
    int statusCode = client.executeMethod(method);
    

    Yet, you’re initialising the javax.net.ssl.HttpsURLConnection with the SSLContext you’ve created:

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    

    This is completely independent of the Apache Http Client settings.

    Instead, you should set up the SSLContext for the Apache Http Client library, as described in this answer. If you’re using Apache Http Client 3.x, you need to set up your own SecureProtocolSocketFactory to use that SSLContext (see examples here). It’s worth upgrading to Apache Http Client 4.x though, which has direct support for SSLContext.

    You can also use Pascal’s answer to import the certificate correctly. Again, if you follow the accepted answer (by Kevin) to that question, you will indeed ignore the error but this will make the connection vulnerable to MITM attacks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used this code in my Android application: import org.apache.http.client.methods.HttpPost; ... HttpClient httpclient =
I have HttpClient 4.1. Please have a look at following program: import org.apache.http.client.methods.*; import
I have the following code from apache's svn. As you can see this is
I'm trying to retrieve this page using Apache HttpClient: http://quick-dish.tablespoon.com/ Unfortunately, when I try
I am trying to set my user agent string in the HttpClient apache object
I am using apache's common httpclient library. Is it possible to make HTTP request
I'm now using Apache CXF as a web services client for a .NET service
I use apache commons http client to send data via post every second, is
I am using org.apache.http.impl.client.DefaultHttpClient to retrieve xml from a webservice and am trying to
I have multiple network interface(Like WIFI,LAN,LTE dougle).I am sending http request using apache httpclient

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.