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); }
source share