In this article, we will show you how to send HTTP Request using GET & POST method using Java API’s.
- javax.net.ssl.HttpsURLConnection
- java.net.HttpURLConnection
1. Java – HttpURLConnection
HttpURLConnection is available in Java base API. Below are the few pros & cons.
- Pros
- No need to add any extra library in your code
- Cons
- Very old API & outdated
- Very difficult to configure and use this class
- May not support HTTP/2 standards
- Not recommended to use
The below example is for self reference however, note that usage of this class and recommended.
package com.techgig365.http;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpURLConnectionExample {
public static void main(String[] args) throws Exception {
HttpURLConnectionExample obj = new HttpURLConnectionExample();
System.out.println("Test 1 - Send Http GET request");
obj.sendGetRequest();
System.out.println("Test 2 - Send Http POST request");
obj.sendPostRequest();
}
private void sendGetRequest() throws Exception {
String url = "https://www.google.com/search?q=techgig365.wordpress.com";
HttpURLConnection httpClient =
(HttpURLConnection) new URL(url).openConnection();
// Optional default is GET
httpClient.setRequestMethod("GET");
// Add request header
httpClient.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = httpClient.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
try (BufferedReader in = new BufferedReader(
new InputStreamReader(httpClient.getInputStream()))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
response.append(line);
}
// Show result
System.out.println(response.toString());
}
}
private void sendPostRequest() throws Exception {
String url = "https://httpbin.org/post";
HttpsURLConnection httpClient = (HttpsURLConnection) new URL(url).openConnection();
// Add request header
httpClient.setRequestMethod("POST");
httpClient.setRequestProperty("User-Agent", "Mozilla/5.0");
httpClient.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
httpClient.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(httpClient.getOutputStream())) {
wr.writeBytes(urlParameters);
wr.flush();
}
int responseCode = httpClient.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
try (BufferedReader in = new BufferedReader(
new InputStreamReader(httpClient.getInputStream()))) {
String line;
StringBuilder response = new StringBuilder();
while ((line = in.readLine()) != null) {
response.append(line);
}
// Show result
System.out.println(response.toString());
}
}
}
