How can send POST request from java program

How can send POST request from java program ?

Answer : Below is sample java program which you could use to send post data. For testing we have used this site to posting sample data and get the results.

package com.javahonk;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;

public class SendPostRequstJava {

    public static void main(String[] args) {
        sendPostRequest();

    }
    
    private static void sendPostRequest() {
        
        URL url = null;
        BufferedReader bufferedReader = null;
        DataOutputStream dataOutputStream = null;
        try {
            
        //If your company has proxy server in place then
        //please enter detail of proxy server to send 
        //request otherwise you will get an error
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new 
                InetSocketAddress("proxy_server_name", 8080));
        url = new URL(" http://posttestserver.com/post.php");
        HttpURLConnection httpURLConnection = 
                (HttpURLConnection) url.openConnection(proxy);
        
        //Without proxy server un-comment below to send request
        //comment out above proxy server configuration
        /*url = new URL("http://www.google.com/search?q=javahonk");
        HttpURLConnection httpURLConnection = 
        (HttpURLConnection) url.openConnection();*/

        httpURLConnection.setRequestMethod("POST");
        
        httpURLConnection.setRequestProperty("User-Agent",
                "Firefox/21.0");
        
        httpURLConnection.setDoOutput(true);
        
        dataOutputStream = new DataOutputStream(httpURLConnection
                .getOutputStream());
        
        dataOutputStream.writeBytes("dump&html&dir=henry"
                + "&status_code=202&sleep=2");

        int responseCode = httpURLConnection.getResponseCode();
        
        System.out.println("Sending request... \n");
        System.out.println("Got response... \n");
        System.out.println("Response Code is: " 
                + responseCode+"\n");
        System.out.println("Read response... \n");

        bufferedReader = new BufferedReader(new 
                InputStreamReader(httpURLConnection.getInputStream()));
        String lineRead;
        StringBuffer response = new StringBuffer();

        while ((lineRead = bufferedReader.readLine()) != null) {
            response.append(lineRead);
        }           

        System.out.println("Response is below... \n");
        System.out.println(response.toString());
        
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                dataOutputStream.flush();
                dataOutputStream.close();
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

 

Output:

How can send POST request from java program

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *