The post will give two examples to send HTTP GET requests using Java standard API or using Apache HttpClient.
1. The example using Java API URLConnection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package net.tecbar.examples; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class HttpGetExamples1 { public String sendGetRequest() { final StringBuilder response = new StringBuilder(); try { // pass parameters in GET request final StringBuilder apiURL = new StringBuilder("http://tecbar.net/demo/param?name=tecbar&group=middleschool"); // apiURL.append("&XML=" + URLEncoder.encode("<name>Tina Limo</name><id>466545</id>", "UTF-8")); final URL url = new URL(apiURL.toString()); final URLConnection conn = url.openConnection(); conn.addRequestProperty("Accept-Language", "en-us"); conn.addRequestProperty("Accept-Encoding", "gzip,deflate"); conn.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8"); conn.addRequestProperty("Keep-Alive", "300"); conn.setConnectTimeout(2500); // in milliseconds // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; while ((str = in.readLine()) != null) { response.append(str); } in.close(); } catch(IOException ex) { // process your exception here } return response.toString(); } public static void main(String[] args) { final HttpGetExamples1 instance = new HttpGetExamples1(); final String response = instance.sendGetRequest(); System.out.println("response :" + response); } } |
2. The example using Apache HttpClient (4.x)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package net.tecbar.examples; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class HttpGetExamples2 { private final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) Version/7.0.3 Safari/7046A194A"; public String sendGetRequest() { final StringBuilder apiURL = new StringBuilder("http://tecbar.net/demo/param?name=tecbar&group=middleschool"); String response = ""; // DefaultHttpClient is deprecated HttpClient httpclient = HttpClientBuilder.create().build(); HttpGet httpget = new HttpGet(apiURL.toString()); httpget.setHeader(HTTP.USER_AGENT, USER_AGENT); try { HttpResponse httpResponse = httpclient.execute(httpget); HttpEntity entity = httpResponse.getEntity(); response = EntityUtils.toString(entity) ; } catch (IOException e) { // process your exception here } return response; } public static void main(String[] args) { final HttpGetExamples2 instance = new HttpGetExamples2(); final String response = instance.sendGetRequest(); System.out.println("response :" + response); } } |