Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamesnguyen101/4fdd3d1c2da379949519b45d424fb258 to your computer and use it in GitHub Desktop.
Save jamesnguyen101/4fdd3d1c2da379949519b45d424fb258 to your computer and use it in GitHub Desktop.
Send HTTP POST/GET request using HttpURLConnection in java
package com.hprog99;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("GET Request Using HttpURLConnection");
http.sendGet();
System.out.println();
System.out.println("POST Request Using HttpURLConnection");
http.sendPost();
}
// HTTP GET request
private void sendGet() throws Exception {
String username="hitenpratap";
StringBuilder stringBuilder = new StringBuilder("https://twitter.com/search");
stringBuilder.append("?q=");
stringBuilder.append(URLEncoder.encode(username, "UTF-8"));
URL obj = new URL(stringBuilder.toString());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestProperty("Accept-Charset", "UTF-8");
System.out.println("\nSending request to URL : " + url);
System.out.println("Response Code : " + con.getResponseCode());
System.out.println("Response Message : " + con.getResponseMessage());
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String line;
StringBuffer response = new StringBuffer();
while ((line = in.readLine()) != null) {
response.append(line);
}
in.close();
System.out.println(response.toString());
}
private void sendPost() throws Exception {
StringBuilder tokenUri=new StringBuilder("param1=");
tokenUri.append(URLEncoder.encode("params1","UTF-8"));
tokenUri.append("&param2=");
tokenUri.append(URLEncoder.encode("param2","UTF-8"));
tokenUri.append("&param3=");
tokenUri.append(URLEncoder.encode("param3","UTF-8"));
String url = "https://example.com";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "UTF-8");
con.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(con.getOutputStream());
outputStreamWriter.write(params.toString());
outputStreamWriter.flush();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment