Issues with HttpClient 4.3.1 creating instance of HttpClient

11,460

Solution 1

The constructor HttpClientBuilder() is protected (not public) so you cannot call it from your code. That is what not visible means.

You invoke the builder using a static constructor method, as such:

HttpClientBuilder.create();

Or, to quickly create a client (which is the whole point)

HttpClient client = HttpClientBuilder.create()
  .setUserAgent("MyAgent")
  .setMaxConnPerRoute(4)
  .build()

Solution 2

You need to use static factory methods from HttpClients.

You can use them to obtain preconfigured instances of HttpClient, or use HttpClients.custom() to apply your custom configuration using HttpClientBuilder.

Share:
11,460

Related videos on Youtube

Brad Ellis
Author by

Brad Ellis

Updated on September 15, 2022

Comments

  • Brad Ellis
    Brad Ellis over 1 year

    I am trying to convert Http upload to use the new HttpClient 4.3.1 classes. I'm new to Java. Everything I find online uses deprecated classes (i.e. HttpClient client = new DefaultHttpClient() or an even older method for creating an instance of HttpClient. Forgive all the extra libraries below, some will be needed in the rest of my project.

    I've tried umpteen different ways to create the instance, what I have below is the method I see used in org.appache documenation for 4.3.1.
    Unfortunately, I'm getting an error indicating that HttpClientBuilder is not visible. I'm not even sure what not visible means...the library has been imported. Can anyone point me in the right direction for creating an HttpClient instance.

     package newHttpApiUpload;
    
     import org.apache.http.client.HttpClient;
     import org.apache.http.HttpConnection;  
     import org.apache.http.conn.HttpClientConnectionManager;
     import org.apache.http.impl.client.HttpClientBuilder;
     import org.apache.http.impl.client.HttpClients;
     import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;  
     import org.apache.http.client.config.RequestConfig; 
     import org.apache.http.auth.AuthScope;
     import org.apache.http.auth.UsernamePasswordCredentials;
     import org.apache.http.client.methods.HttpPost;
     import org.apache.http.entity.AbstractHttpEntity; 
    
     import java.io.File;
     import java.io.FileNotFoundException;
     import java.io.IOException;
     import java.io.UnsupportedEncodingException;
     import java.net.URLEncoder;
    
     public class Api {
    
         protected static final HttpClientBuilder client = new HttpClientBuilder();
    
     }
    
  • Brad Ellis
    Brad Ellis over 10 years
    Should I not be using private static final? the methods suggested "do[es] not resolve to a type." I looked at the methods in HttpClientBuilder and found a method called build() but it returns the same "does not resolve to a type" error. Thanks for taking time to answer!
  • Joeri Hendrickx
    Joeri Hendrickx over 10 years
    your own modifiers will not affect that. "cannot be resolved to a type" is an Eclipse classpath problem and has nothing to do with the code itself.
  • Brad Ellis
    Brad Ellis over 10 years
    Ah okay. oh lawd...troubleshooting a class path issue sounds like a great time. I fumbled through the build class path and and eclipse class path documentation...Lost. :) Oh well...the joys of learning. Thanks again!
  • Brad Ellis
    Brad Ellis over 10 years
    Ultimately I needed to use: HttpClient httpclient = HttpClients.createDefault()