Search code examples
javahttpclienthttpurlconnectionfiddlerapache-httpclient-4.x

Replay a post request using Java


I want to replay a POST request using java. I have got all the headers from Fiddler. Here is the scrambled copy of it.

POST http://www.website.com/cgi_bin/abc.cgi HTTP/1.1
Host: www.website.com
Connection: keep-alive
Content-Length: 81
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://www.website.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://www.website.com/page_one.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,hi;q=0.6,ms;q=0.4,ta;q=0.2
Cookie: _ga=GA1.3.5137840677.9397628959

parm1=4611&parm2=818&parm3=818&submit=Find+Status

I tried setting the above properties in multiple ways, but it didn't work if I programmed but I am able to reply in fiddler successfully.

How to do that? If possible please provide solution using Apache HTTP Client.

Here is the code which I tried.

import java.io.*;
import java.net.*;

public class Main {
    public static void main(String[] args) {
        try{            
            URL url = new URL("http://www.website.com/cgi_bin/abc.cgi");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();

            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0");
            conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            conn.setRequestProperty("Cookie", "_ga=GA1.3.5137840677.9397628959");
            conn.setRequestProperty("Referer", "http://www.website.com/page_one.html");
            conn.setRequestProperty("Origin", "http://www.website.com");
            conn.addRequestProperty("parm1", "4611");
            conn.addRequestProperty("parm2", "818");
            conn.addRequestProperty("parm2", "818");
            conn.addRequestProperty("submit", "Find Status");

            conn.connect();

            InputStream  is = conn.getInputStream();
            FileOutputStream fos = new FileOutputStream("output.html");

            int numCharsRead;
            byte[] byteArray = new byte[1024];

            while ((numCharsRead = is.read(byteArray)) > 0) {               
                fos.write(byteArray, 0, numCharsRead);
            }

            fos.close();

            System.out.println("Done!");

        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
}

Solution

  • Apache HttpClient can potentially solve your problem. Below code snippet might help you but you need to make necessary customization to this.

    Note: Also refer HttpClient and HttpCore

            HttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("Your URL");
    
            //add your headers like this
            httpPost.setHeader("Content-type", "application/xml");
            httpPost.setHeader("Accept", "application/xml");
    
            //add your parameters like this
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("param1", "4611"));
            nameValuePairs.add(new BasicNameValuePair("param2", "818"));
            nameValuePairs.add(new BasicNameValuePair("param3", "818"));
            nameValuePairs.add(new BasicNameValuePair("submit", "Find Status"));
    
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
            HttpResponse response = client.execute(httpPost); 
            HttpEntity httpEntity = response.getEntity();
    
            //get response as String or what ever way you need
            String response = EntityUtils.toString(httpEntity);