Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get raw post reply from Jsoup

Tags:

java

http

jsoup

I am using Jsoup to post a form to a website, the reply is plain text, but the content type isn't known to Jsoup. Therefore I use ignoreContentType(true), so Jsoup won't throw an exception because of the unknown content type. However I can't figure out how to get the raw reply from Jsoup, the text contains some special characters, that Jsoup strips, also Jsoups adds a body and html tag - I don't need that, I just want the raw reply. How to get the raw reply? Thanks!

like image 750
stefan.at.wpf Avatar asked Mar 25 '12 19:03

stefan.at.wpf


2 Answers

You could use Response#body() for this. E.g.

String body = Jsoup.connect(url).execute().body();

Or if you're doing POST

String body = Jsoup.connect(url).method(Method.POST).data(data).execute().body();
like image 154
BalusC Avatar answered Nov 05 '22 16:11

BalusC


You can get the Response object from Connection, and it contains the raw byte data. Ignore content type or it will fail on non-text data. If you use body() it may mangle the response using the default charset.

Connection conn = Jsoup.connect(url).ignoreContentType(true).method(Method.GET);
Connection.Response response = conn.execute();
byte[] raw = response.bodyAsBytes();

// if you want...
Document parsed = response.parse();
like image 24
engineerC Avatar answered Nov 05 '22 15:11

engineerC