Need Java Expert Help on Executing HTTP GET with Cookie -
my problem want use java implement application sends http request website. however, target website needs 1 cookie set:
shippingcountry=us
if cookie not set returns bad indications. below code segment , null 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); 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 output stream out.close();
}
can me?
just guess perhaps might need setrequestproperty
instead of addrequestproperty
, since there can 1 cookie string in request.
connection.setrequestproperty("cookie", "shippingcountry=us");
if need send multiple cookie values tack them on colons:
connection.setrequestproperty("cookie", "shippingcountry=us;othervalue=2");
update:
i tried doing request using python , looks 500 error. perhaps server blocking request because doesn't web browser. maybe adding following headers work:
user-agent: mozilla/5.0 (macintosh; intel mac os x 10_7) applewebkit/534.48.3 (khtml, 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 accept
, user-agent
headers might required.
Comments
Post a Comment