users@jersey.java.net

How do you configure proxy in Jersey client

From: Sverre Kristoffer Furseth <sverre.kristoffer.furseth_at_gmail.com>
Date: Thu, 2 Sep 2010 11:15:31 +0200

Do not find any good information on google , hence this mail ....

Refering to test attached underneath :
 - http client apache works : SUCCESS
 - jersey client do not work : FAIL

I have tried setting proxy in the Java Console and as properties with
-D option as
"-Dhttp.proxyHost=proxy.ergogroup.no -Dhttp.proxyPort=3128"

without any luck

Versions in use :
<jersey-client.version>1.1.5</jersey-client.version>
<commons-net.version>2.0</commons-net.version>

TEST :
---8<--------
package no.my.client.proxy;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.GetMethod;
import org.junit.Test;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;

public class SimpleProxyTest {

        @Test
        public void testSimple() throws Exception {
                System.out.println("test");
                
                
                  HttpClient httpclient = new HttpClient();
                  httpclient.getHostConfiguration().setProxy("proxy.ergogroup.no", 3128);
                  GetMethod httpget = new GetMethod("https://www.verisign.com/");
                  try {
                    httpclient.executeMethod(httpget);
                    System.out.println(httpget.getStatusLine());
                  } finally {
                    httpget.releaseConnection();
                  }
        }
        
        @Test
        public void testSimpleJerseyClient() throws Exception {
                System.out.println("test");
                DefaultApacheHttpClientConfig cc = new DefaultApacheHttpClientConfig();
                cc.getProperties().put(DefaultApacheHttpClientConfig.PROPERTY_PROXY_URI,"proxy.ergogroup.no:3128");
                Client c = Client.create(cc);
                WebResource webResource =
c.resource("http://www.balder.no/images/sn_banner_180x100px.gif");
                ClientResponse response = webResource.get(ClientResponse.class);
                System.out.println(response.getHeaders());
        }
}