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();
}
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With