158

How do I do a HTTP GET in Java?

2

4 Answers 4

241

If you want to stream any webpage, you can use the method below.

import java.io.*;
import java.net.*;

public class c {

   public static String getHTML(String urlToRead) throws Exception {
      StringBuilder result = new StringBuilder();
      URL url = new URL(urlToRead);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      try (BufferedReader reader = new BufferedReader(
                  new InputStreamReader(conn.getInputStream()))) {
          for (String line; (line = reader.readLine()) != null; ) {
              result.append(line);
          }
      }
      return result.toString();
   }

   public static void main(String[] args) throws Exception
   {
     System.out.println(getHTML(args[0]));
   }
}
Sign up to request clarification or add additional context in comments.

9 Comments

One of the advantages of cletus's answer (using Apache HttpClient) is that HttpClient can automatically handle redirects and proxy authentication for you. The standard Java API classes that you use here don't do that for you. On the other hand, using the standard API classes has the advantage that you don't need to include a third-party library in your project.
Also the URL class is unable to get the charset for decoding the result.
Good example but it's better to catch IOException instead of "general" Exception.
It's necessary to set a timeout or the current thread may be blocked. See setConnectTimeout and setReadTimeout.
The above solution makes the length of the read equal to the line length even though HTML does not, AFAIK, have the concept of a line. It also discards CR and LF characters. An alternative: int readSize = 100000; int destinationSize = 1000000; char[] destination = new char[destinationSize]; int returnCode; int offset = 0; while ((returnCode = bufferedReader.read(destination, offset, readSize)) != -1) { offset += returnCode; if (offset >= destinationSize) throw new Exception(); } bufferedReader.close(); return (new String(destination)).substring(0, offset+returnCode+1);
|
58

Technically you could do it with a straight TCP socket. I wouldn't recommend it however. I would highly recommend you use Apache HttpClient instead. In its simplest form:

GetMethod get = new GetMethod("http://httpcomponents.apache.org");
// execute method and handle any error responses.
...
InputStream in = get.getResponseBodyAsStream();
// Process the data from the input stream.
get.releaseConnection();

and here is a more complete example.

1 Comment

This project is end of live.
40

If you dont want to use external libraries, you can use URL and URLConnection classes from standard Java API.

An example looks like this:

String urlString = "http://wherever.com/someAction?param1=value1&param2=value2....";
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
// Do what you want with that stream

4 Comments

@HyLian: given the apparent level of the OP's question, your code fragment should include a try { } finally { } to tidy up.
@Stephen C: For sure, that was only a code fragment to show what classes are in the game and how to use them. If you put that in a real program you should play the exception rules :)
InpuTSteam = all the gets the server sends to us?
You need to include the 'GET' part of the question - this misses the GET - see answer below
9

The simplest way that doesn't require third party libraries it to create a URL object and then call either openConnection or openStream on it. Note that this is a pretty basic API, so you won't have a lot of control over the headers.

Comments