Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

Accepting a cookie and using it

Thread view: 
Rolf van der Vleuten - 08 Jan 2008 16:40 GMT
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);
derek - 08 Jan 2008 17:33 GMT
> Hi,
> I have read a lot of posts here, none of them seems to resolve my
[quoted text clipped - 7 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.