使用Java重播帖子请求

时间:2014-07-03 11:31:21

标签: java httpclient httpurlconnection fiddler apache-httpclient-4.x

我想使用java重播POST请求。我有来自Fiddler的所有标题。这是它的混乱副本。

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

我尝试以多种方式设置上述属性,但如果我编程但是我能够成功回复fiddler则无效。

怎么做?如果可能,请使用 Apache HTTP Client 提供解决方案。

这是我试过的代码。

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());
        }
    }
}

1 个答案:

答案 0 :(得分:4)

Apache HttpClient可以解决您的问题。下面的代码段可能对您有所帮助,但您需要对此进行必要的自定义。

注意:另请参阅HttpClient和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);