10

.NET has the HttpWebRequest and WebClient classes for simulating a browser's requests.

I'd google it, but I'm not sure what keyword to use.

I want to write code that does does HTTP GETs and POSTs, along with cookies, in an applet or local .jar and gives me back the response in a text string or some other parseable structure.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164

4 Answers4

11

HttpURLConnection is Java's equivalent of HttpWebRequest.

URL iurl = new URL(url);
HttpURLConnection uc = (HttpURLConnection)iurl.openConnection();
uc.connect();
if (uc.getContentType().equalsIgnoreCase("image/jpeg"))
{
  result = true;
}
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
7

Apache HTTPClient has equivalent functionality, though the APIs are not exactly the same. Oakland Software has a table comparing their commercial product with various alternatives, including the Apache product. Apache's own opinion of the built-in HttpUrlConnection (quoted from the above linked-to page) is:

The jdk has the HttpUrlConnection which is limited and in many ways flawed.

Here's a link to the HTTPClient tutorial.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
1

html unit for me. i can simulate javascript (to a certain extent)

Titi Wangsa bin Damhore
  • 7,101
  • 4
  • 31
  • 36
  • I think he's actually referring to httpunit: http://httpunit.sourceforge.net/doc/api/com/meterware/httpunit/WebClient.html – jeckhart Mar 15 '11 at 16:15
1

Verify Webclient in Apache Cx JaxRs Library.

Checkout this: https://cxf.apache.org/javadoc/latest/org/apache/cxf/jaxrs/client/WebClient.html

Sample code looks below:

WebClient client = WebClient.create(url);
client.path(ADD_PATH).path("/books/2").accept("text/plain");
s = client.get(String.class);
System.out.println(s);
Vallabha Vamaravelli
  • 1,153
  • 1
  • 9
  • 15