Press enter to see results or esc to cancel.


JAVA send http Get Post request with basic authentication

In the video I have shown how you can send HTTP get request and Post request with a real time example.
The server is giving response in JSON format. The JSON response is parsed with java to get the parameters output.

This source code is demonstrated with reddit login API documents. You will have to add the below jar files to the class path to work the program.
1. java-json.jar
2. org-apache-commons-codec.jar
3. javax.servlet-3.0.jar

Send_Post_Basic_Auth.java

package com.chillyfacts.com;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;
public class Send_Post_Basic_Auth {
 public static void main(String[] args) {
 try {
   String url = "url here";
   URL obj = new URL(url);
   HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
   con.setRequestMethod("POST");
 
   String user_name="";
   String password="";
 
   String userCredentials = user_name+":"+password;
   String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
   con.setRequestProperty ("Authorization", basicAuth);
   con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:27.0) Gecko/20100101 Firefox/27.0.2 Waterfox/27.0");
   con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
   String urlParameters = "param1=value1&param2=value2";
   // Send post request
   con.setDoOutput(true);
   DataOutputStream wr = new DataOutputStream(con.getOutputStream());
   wr.writeBytes(urlParameters);
   wr.flush();
   wr.close();
   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();
   //print result
   System.out.println(response.toString());


   // below code converts the json response to json object and reads each values
   JSONObject jsonObj = new JSONObject(response.toString());
   String access_token =jsonObj.getString("access_token");
   System.out.println("access_token : "+access_token);
   } catch (Exception e) {
    // TODO: handle exception
   }
}
}

Send_Get_Basic_Auth.java

package com.chillyfacts.com;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class Send_Get_Basic_Auth {
 public static void main(String[] args) {
 try {
   String access_token="";
   String url = "url here";
   URL obj = new URL(url);
   HttpURLConnection con = (HttpURLConnection) obj.openConnection();
   // optional default is GET
   con.setRequestMethod("GET");
   con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:27.0) Gecko/20100101 Firefox/27.0.2 Waterfox/27.0");
   con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
   con.setRequestProperty("Authorization", "Bearer "+ access_token);
   int responseCode = con.getResponseCode();
   System.out.println("\nSending 'GET' request to URL : " + url);
   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();
   //print result
   System.out.println(response.toString());


   // below code converts the json response to json object and reads each values
   JSONObject jsonObj = new JSONObject(response.toString());
   System.out.println(jsonObj.getString("id"));
   System.out.println(jsonObj.getString("name"));
   } catch (Exception e) {
    System.out.println(e);
  }
 }
}

Comments

Comments are disabled for this post