Question

I need to be able to use a password protected proxy and be able to read json information returned from a url.

I do not want to declare proxies at the system level; I would like to have multiple proxies being used in the same application.

What is the best way to do this?

Was it helpful?

Solution

I once faces the same problem. Unfortunately, JSoup is not a good choice for this. I ended up using the apache http client, which works nicely with proxies.

Here is the proxy-relevant part of my http-client configuration:

String ipStr = "the.proxy.ip.string";
int port = 8080;
String proxyLogin = "your login name";
String proxyPassword = "your password";

httpClient.getCredentialsProvider().setCredentials(
    new AuthScope(ipStr, port),
    new UsernamePasswordCredentials(proxyLogin, proxyPassword));

HttpHost httpHost = new HttpHost(ipStr, port, "http");      
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);

You can use the http-client to get the website or JSON response from the net. If the content is HTML, you can use JSoup as parser with the returned input. If you get JSON back, then you probably want to use a JSON parser like json-simple (but there are many other very useful JSON libraries out there!)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top