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?

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.