开发者

Need Java Expert Help on Executing HTTP GET with Cookie

My problem is that I want to use Java to implement an application which sends an HTTP GET request to some website. However, the target website needs one cookie to be set:

ShippingCountry=US

If this cookie is not set it returns bad indications. Below are my code segment and I get null from connect().

String urlString = "http://www1.macys.com/catalog/index.ognc?CategoryID=5449&viewall=true";
    try{
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    connection.addRequestProperty("Cookie", "ShippingCountry=US");
    connection.connect();

    // Create file 
    FileWriter fstream = new FileWriter("d:/out.txt");
    BufferedWriter out = new BufferedWriter(fstream)开发者_如何学Go;
    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuffer sb = new StringBuffer();
    String line;
    while ((line = rd.readLine()) != null)
    {
        out.write(line);
    }
    rd.close();
    //Close the output stream
    out.close();

}

Can somebody help me?


Just a guess but perhaps you might need setRequestProperty instead of addRequestProperty, since there can be only one Cookie string in a request.

connection.setRequestProperty("Cookie", "ShippingCountry=US");

If you need to send multiple cookie values you tack them on with colons:

connection.setRequestProperty("Cookie", "ShippingCountry=US;OtherValue=2");

Update:

I tried doing a GET request using python and it looks like I get a 500 error. Perhaps the server is blocking the request because it doesn't look like a web browser. Maybe adding the following headers will work:

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7) AppleWebKit/534.48.3 (KHTML, like Gecko) Version/5.1 Safari/534.48.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: keep-alive

In particular the Accept and User-Agent headers might be required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜