Java Shell
Clone or download
Latest commit 32ddd07 Aug 14, 2014

mechanize for java

mechanize is a stateful HTTP web services client library (RESTful) with support for HTML, JSON, and XML services. Because mechanize is stateful and allow inspection of web resources, it naturally support GET-then-POST semantics and helps avoid hard-coding clients.

See the Mechanize Wiki for more details and opinions on how RESTful clients should behave.

====== Releases:

  • 1.x is the original port of mechanize to java. This will be maintained for bug fixes and possibly other enhancements.
  • 2.x is the modularized version to separate HTML, JSON, and XML into separate a la carte packages. Also the JSON linking dev will be here.


Here is a testcase showing mechanize at work with HTML:

	@Test public void testGooglePageSearchForm() {
		MechanizeAgent agent = new MechanizeAgent();
		Document page = agent.get("");
		Form form = page.form("f");
		form.get("q").set("mechanize java");
		Resource response = form.submit();
		assertTrue(response.getTitle().startsWith("mechanize java"));

Here is a testcase showing mechanize using a Google JSON web service:

	public void testGoogleApi() throws JSONException {
		MechanizeAgent agent = new MechanizeAgent();
		JsonDocument json = agent.doRequest(googleUrl).add("shortUrl", shortUrl).add("projection", "FULL").get();

		assertEquals(longUrl, json.getRoot().find("longUrl").getValue());

		String value = json.getRoot().find("analytics month countries#US count").getValue();
		assertTrue(value, Integer.valueOf(value)>=1);


Include this library (or download) from the Maven repository: