How do I do a HTTP GET in Java?
Asked
Active
Viewed 4.5e+01k times
154
-
2http://stackoverflow.com/q/2793150/632951 – Pacerier Jul 14 '12 at 21:21
-
1You can also use **Java 11** new [HTTP Client API](https://openjdk.java.net/groups/net/httpclient/intro.html). See [this post](https://stackoverflow.com/a/70545055/8583692). – Mahozad Dec 31 '21 at 19:10
4 Answers
238
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]));
}
}
-
9One 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. – Jesper Sep 28 '09 at 08:10
-
1Also the URL class is unable to get the charset for decoding the result. – Nick Bolton Nov 03 '11 at 19:21
-
Thanks Duncan, Will keep in mind going forward. Will not use the pre tag and tabs. – Kalpak Oct 20 '13 at 04:57
-
7Good example but it's better to catch IOException instead of "general" Exception. – adalpari Nov 06 '13 at 12:05
-
-
5It's necessary to set a timeout or the current thread may be blocked. See `setConnectTimeout` and `setReadTimeout`. – Anderson Jan 09 '14 at 03:54
-
Perfect, I just wanted to add, that the call must include the protocol ie: HTTP and the URL. ie: www.somepage.com – Nikita Yo LAHOLA Jun 09 '15 at 02:44
-
1The 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);` – H2ONaCl Feb 17 '18 at 01:33
-
59
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.

Valentin Jacquemin
- 2,215
- 1
- 19
- 33

cletus
- 616,129
- 168
- 910
- 942
41
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¶m2=value2....";
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
// Do what you want with that stream

Senura Dissanayake
- 654
- 1
- 9
- 29

HyLian
- 4,999
- 5
- 33
- 40
-
1@HyLian: given the apparent level of the OP's question, your code fragment should include a try { } finally { } to tidy up. – Stephen C Sep 28 '09 at 06:52
-
@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 :) – HyLian Sep 28 '09 at 08:01
-
-
You need to include the 'GET' part of the question - this misses the GET - see answer below – Feb 03 '17 at 11:18
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.

Laurence Gonsalves
- 137,896
- 35
- 246
- 299