xxx
xxx

Reputation: 45

Java multithreaded post method with proxy (using Jsoup)

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?

Upvotes: 2

Views: 694

Answers (1)

wassgren
wassgren

Reputation: 19211

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:

Upvotes: 1

Related Questions