Skip to content

Instantly share code, notes, and snippets.

@hitenpratap
Last active August 9, 2023 12:25
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save hitenpratap/8e1f28d60c0bb11a5bca to your computer and use it in GitHub Desktop.
Save hitenpratap/8e1f28d60c0bb11a5bca 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());
}
}
@Marcelo-Machado
Copy link

I was able to post the post, but I can not read the json return in php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment