Using proxies with the Google HTTP Client Library for Java

I use the Google HTTP client library for Java to make simple JSON requests and parse the responses. It works well when I do not go through the proxy. But now I want to allow my users to use the proxy function (with authentication) in my application. I looked through the HttpTransport, HttpRequestFactory, and HttpRequestInitializer classes without much difficulty.

I have only changed the examples a bit so far (and basically it removed the unnecessary code). So where in the code am I adding proxy settings?

static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); static final JsonFactory JSON_FACTORY = new JacksonFactory(); <T> T get(String url, Class<T> type) throws IOException { HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { @Override public void initialize(HttpRequest request) { request.setParser(new JsonObjectParser(JSON_FACTORY)); } }); HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(url)); return request.execute().parseAs(type); } 
+4
source share
1 answer

This seems to work fine for an authenticated proxy using google-http-client: 1.13.1-beta p>

 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT)); HttpTransport httpTransport = new NetHttpTransport.Builder().setProxy(proxy).build(); 

Is this not enough for your needs?

+5
source

All Articles