Hi I've a jersey client which i use to upload a file. I tried using it locally and everything works fine. But in production environment i've to set proxy. I browsed thru few pages but could not get exact solution. Can someone pls help me with this?
here is my client code:
File file = new File("e:\\test.zip");
FormDataMultiPart part = new FormDataMultiPart();
part.bodyPart(new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
WebResource resource = null;
if(proxy.equals("yes")){
//How do i configure client in this case?
}else{
//this uses system proxy i guess
resource = Client.create().resource(url);
}
String response = (String)resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
System.out.println(response);
luckyluke answer shall work. Here my version:
There is an easier approach, if you want to avoid more libraries in legacy projects and there is no need for Proxy Authentication:
First you need a class which implements
HttpURLConnectionFactory
:Second is to instantiate an
com.sun.jersey.client.urlconnection.URLConnectionHandler
and third is to use the
Client
Constructor instead ofClient.create
:Of course you can customize the initializing of the Proxy in the
ConnectionFactory
.SDolgy. I did this who add 3 features in the Jersey client instantiation: Enables SSL TLSv1.1 (JVM >= 1.7 needed), Configure conex. pooling. to increase conections, Set a system proxy.
Here you go:
First of all I created this class
then I also create another class called SecureTrustManager
then after creation this class i'm calling the client like this
replace webProxy.getWeserviceHost by your proxyHost and webProxy.getWebserviceProxyPort() by the proxy port.
This worked for me and it should work also for you. Note that i'm using Jersey 1.8 but it should also work for Jersey 2