2

Hi i need to know how to use proxy with Jsoup in multithreaded application. When i try this:

System.setProperty("http.proxyHost", myproxy); 
System.setProperty("http.proxyPort", myport);

It's set proxy for all threat i made, i need to each threat use own proxy. This GET method work good:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("addres", port));
URL url = new URL("address");
URLConnection connect = url.openConnection(proxy);

BufferedReader br = new BufferedReader(new InputStreamReader(connect.getInputStream()));

String tmp;
StringBuilder sb = new StringBuilder();
while((tmp=br.readLine())!=null) sb.append(tmp);

Document c = Jsoup.parse(sb.toString());

But I don't know how to send POST method using proxy in each threat with Jsoup. Can someone help me?

xxx
  • 45
  • 5

1 Answers1

1

From the JSoup docs:

jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.

So, basically Jsoup is created for extracting data. However, it is still possible to execute POST requests but it is not as straight forward as for GET requests.

ocument doc = Jsoup.connect("http://www.facebook.com")
                   .data("field1", "value2")
                   .data("field2", "value2")
                   .userAgent("Mozilla") // Optional
                   .post();

In order to work this out with a proxy the following can be used:

System.setProperty("http.proxyHost", "<proxy-host>");
System.setProperty("http.proxyPort", "<proxy-port>");

Or, for the https equivalent:

System.setProperty("https.proxyHost", "<proxy-host>");
System.setProperty("https.proxyPort", "<proxy-port>");

There are a bunch of other questions on StackOverflow regarding this matter. Check out:

Community
  • 1
  • 1
wassgren
  • 18,651
  • 6
  • 63
  • 77
  • Actually you can Post using Jsoup. You can login a webpage and save the session cookie for further browsing, I'm doing it in one of my applications. But I reached this post looking for the Proxy solution, I have the very same issue – McCoy Jan 17 '15 at 03:48