Search code examples
java.netlanguage-comparisons

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


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


Solution

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