Equivalent of .NET's WebClient and HttpWebRequest in Java?

MatthewMartin picture MatthewMartin · Jul 16, 2009 · Viewed 28.8k times · Source

.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.

Answer

Mitch Wheat picture Mitch Wheat · Jul 16, 2009

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;
}