Sunday 3 September 2017

Post data to url using HttpURLConnection

By writing to HttpURLConnection output stream, you can send post data to http url.

Below step-by-step procedure explains how to post the data.

Step 1: Get HttpURLConnection instance.
                 URL url = new URL(urlToConnect);
                 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
                
Step 2: Set the request method (POST, PUT etc.,) and other request headers.
                 httpUrlConnection.setRequestMethod(method);
                 httpUrlConnection.setDoOutput(true);
                 httpUrlConnection.setRequestProperty("Content-Type", requestContentType);

Step 3: Write the data to HttpUrlConnection output stream. After writing the data flush and close the stream.
                 OutputStreamWriter writer = new OutputStreamWriter(httpUrlConnection.getOutputStream());
                 writer.append(content);
                 writer.flush();
                 writer.close();
                
Step 4: Get the content stream.
                 int responseCode = httpUrlConnection.getResponseCode();
                 InputStream inputStream = null;

                 if (responseCode >= 200 && responseCode < 400) {
                          inputStream = httpUrlConnection.getInputStream();
                 } else {
                          inputStream = httpUrlConnection.getErrorStream();
                 }

Step 5: Print the stream to console.
                 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

                 String line = null;

                 while ((line = br.readLine()) != null) {
                          System.out.println(line);
                 }
        

Find below working application.


Test.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

 private static HttpURLConnection getURLConnection(String urlToConnect) throws IOException {
  URL url = new URL(urlToConnect);
  HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
  return httpUrlConnection;
 }

 private static HttpURLConnection writeContentTo(String urlToWrite, String content, String requestContentType,
   String method) throws IOException {
  HttpURLConnection httpUrlConnection = getURLConnection(urlToWrite);

  httpUrlConnection.setRequestMethod(method);
  httpUrlConnection.setDoOutput(true);
  httpUrlConnection.setRequestProperty("Content-Type", requestContentType);
  OutputStreamWriter writer = new OutputStreamWriter(httpUrlConnection.getOutputStream());
  writer.append(content);
  writer.flush();
  writer.close();

  return httpUrlConnection;
 }

 private static InputStream getContentStream(HttpURLConnection httpUrlConnection) throws IOException {
  int responseCode = httpUrlConnection.getResponseCode();
  InputStream inputStream = null;

  if (responseCode >= 200 && responseCode < 400) {
   inputStream = httpUrlConnection.getInputStream();
  } else {
   inputStream = httpUrlConnection.getErrorStream();
  }

  return inputStream;
 }
 
 private static void printInputStream(InputStream inputStream) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

  String line = null;

  while ((line = br.readLine()) != null) {
   System.out.println(line);
  }
 }


 public static void main(String[] args) throws IOException {
  String url = "urlToWrite";
  String method = "POST";
  String content = "{\"id\" : \"krishna\", \"logonId\" : \"hari\"}"; //you can write any content
  String requestContentType = "application/json";

  HttpURLConnection httpUrlConnection = writeContentTo(url, content, requestContentType, method);
  InputStream inputStream = getContentStream(httpUrlConnection);
  
  printInputStream(inputStream);
  
 }

}


Previous                                                 Next                                                 Home

No comments:

Post a Comment