Keannylen
Keannylen

Reputation: 483

How to execute HTTP GET method with cookie value filled?

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:

Country=US

If this cookie is null it returns bad indications. My question is how I can set the cookie value before I use openConnection()?

Upvotes: 0

Views: 976

Answers (2)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

You can place the cookie your self by adding a header, or use a higher level HTTP library like Apache's HttpClient which API includes cookies handling features.

Upvotes: 0

Peter Knego
Peter Knego

Reputation: 80340

You can use URLConnection and add a Cookie header:

http://www.hccp.org/java-net-cookie-how-to.html

URL myUrl = new URL("http://www.yourserver.com/path");
URLConnection urlConn = myUrl.openConnection();
urlConn.setRequestProperty("Cookie", "Country=US");
urlConn.connect();

Upvotes: 1

Related Questions