The support for sending and processing HTTP requests was always very basic in the JDK. There are many, many frameworks out there for sending requests and handling or parsing the response. But IMHO two stand out: HTTPClient for sending and HTMLUnit for handling. And since HTMLUnit uses HTTPClient under the hood the two are a perfect match.
This is an example HTTP Post:
HttpClient client = new HttpClient(); PostMethod post = new PostMethod(url); for (Entry param : params.entrySet()) { post.setParameter(param.key, param.value); } try { return client.executeMethod(post); } finally { post.releaseConnection(); }
and HTTP Get:
WebClient webClient = new WebClient(); return (HtmlPage) webClient.getPage(url);
Accessing the returned HTML via XPath is also very straightforward:
List roomDivs=(List)page.getByXPath("//div[contains(@class, 'room')]"); for (HtmlElement div:roomDivs) { rooms.add( new Room(this, ((HtmlElement) div.getByXPath(".//h2/a").get(0)).getTextContent(), div.getId()) ); }
One last issue remains: HTTPClient caches its cookies but HTMLUnit creates a HTTPClient on its own. But if you override HttpWebConnection and give it your HTTPClient everything works smoothly:
public class HttpClientBackedWebConnection extends HttpWebConnection { private HttpClient client; public HttpClientBackedWebConnection(WebClient webClient, HttpClient client) { super(webClient); this.client = client; } @Override protected HttpClient getHttpClient() { return client; } }
Just set your custom webconnection on your webclient:
webClient.setWebConnection( new HttpClientBackedWebConnection(webClient, client) );
Welcome to 3 years ago.
Well, I just happened to find it in a current project and wanted to share it. It is also an example that elegant code in Java is possible despite the global mood towards Java.
If it is water under the bridge for you maybe our other posts help you more.
thanks for it, i’m studying how to send HTTP request in Java.