Accepting a cookie and using it

  • Thread starter Rolf van der Vleuten
  • Start date
R

Rolf van der Vleuten

Hi,

I have read a lot of posts here, none of them seems to resolve my
problem.

I want to make a connection to a site, login using GET variables.

If I directly call the site like : https://www.site.com/?username=me&password=mypass
then i get a page telling me to turn on cookie support.

To solve this I guess i have to include a cookie from 'site.com' in my
request.

When I execute this code, i still get the 'you dont accept cookies'
page.
When I examine the headers there is no "cookie" attribute

so here is what i did :

//first obtain a cookie by requesting the login page
URL url = new URL(GET_COOKIE_URL);

HttpURLConnection urlconnection = (HttpURLConnection)
url.openConnection();

// Get the cookie
String cookie = urlconnection.getHeaderField("Set-Cookie");

// everything after ; can go
int index = cookie.indexOf(";");
if (index >= 0) {
cookie = cookie.substring(0, index);
}

// Up to the next page (https://www.site.com/?
username=me&password=mypass)
url = new URL(LOGIN_CONNECT_URL);

urlconnection = (HttpURLConnection) url.openConnection();

// set the cookie
urlconnection.setDoOutput(true);
urlconnection.setRequestProperty("cookie", cookie);
logger.info("Cookie set as : " + cookie);

OutputStream rawOutStream = urlconnection.getOutputStream();
PrintWriter pw = new PrintWriter(rawOutStream);

pw.print(""); // here we "send" our body!
pw.flush();
pw.close();

InputStream is = urlconnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bufRead = new BufferedReader(isr);
 
D

derek

Hi,
I have read a lot of posts here, none of them seems to resolve my
problem.
I want to make a connection to a site, login using GET variables.
If I directly call the site like :
https://www.site.com/?username=me&password=mypass
then i get a page telling me to turn on cookie support.
To solve this I guess i have to include a cookie from 'site.com' in my
request.
When I execute this code, i still get the 'you dont accept cookies'
page.

Dont reinvent the wheel. Use an existing library for this.
For example, use apache commons http://hc.apache.org/
I have used it myself and found it very good.
It will handle all of your cookie needs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top