Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Post Deleting Cookies in Java

    I'm using an URLConnection to log into a site and I want to clear all of the cookies for that site every time I reconnect to the URLConnection.

    How can I go about doing this?

    Thanks,
    ~Solarsonic

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    I wonder if you can tell the server to clear your cookies. ???
    My idea of how cookies work is that they are stored in your browser. When the browser connects to a server it sends all the cookies it has for that site. If you have saved any cookies from your previous connection to a site you could clear them and then send them to the site.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    I wonder if you can tell the server to clear your cookies. ???
    My idea of how cookies work is that they are stored in your browser. When the browser connects to a server it sends all the cookies it has for that site. If you have saved any cookies from your previous connection to a site you could clear them and then send them to the site.
    Yes, how would I go about doing this? I have no clue :/

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Look at the contents of a HTML request header. It contains the cookies that are being sent to the server.

    I have no idea how your code would save cookies. Does it get cookies from the site when it connects for login?
    Can you explain all of your app in more detail?

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Look at the contents of a HTML request header. It contains the cookies that are being sent to the server.

    I have no idea how your code would save cookies. Does it get cookies from the site when it connects for login?
    Can you explain all of your app in more detail?
    I'm trying to login to a site using an URLConnection and proxy to bypass a captcha. When I proxy, the captcha still stays for some reason...

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Sorry, I have no idea how the site you are using works.

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Sorry, I have no idea how the site you are using works.
    Do URLConnections save/use cookies? And if I want to delete cookies for a certain site, how could I do so?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Where would the URLConnection class get the cookie from if you are connecting for the first time?
    Have you tried using the URLConnection methods to look at the contents of the header fields?

  9. #9
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Where would the URLConnection class get the cookie from if you are connecting for the first time?
    Have you tried using the URLConnection methods to look at the contents of the header fields?
    Yes, and I retrieved the cookie names and values. Now how can I delete them?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Does the URLConnection class send cookies when it connects to a server?

  11. #11
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Does the URLConnection class send cookies when it connects to a server?
    I'm not sure; I think so, that's kind of what I'm asking you.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Do you have any software you can run to show what is sent and received?

    How would URLConnection get any cookies to send on its first usage?

    Here's what is sent by a simple program:
    GET / HTTP/1.1
    User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)
    Host: 127.0.0.1:8080
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive
    That's sent by this code:
    Java Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
     
    public class GrabHTML {
     
     public static void Connect() throws Exception{
     
      //Set URL
      URL url = new URL("http://127.0.0.1:8080");   // Connect to my server
      URLConnection spoof = url.openConnection();
     
      //Spoof the connection so we look like a web browser
      spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
      BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
      String strLine = "";
     
      //Loop through every line in the source
      while ((strLine = in.readLine()) != null){
     
       //Prints each line to the console
       System.out.println(strLine);
      }
     
      System.out.println("End of page.");
     }
     
     public static void main(String[] args){
     
      try{
       //Calling the Connect method
       Connect();
      }catch(Exception e){
     
      }
     }
    }

  13. #13
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Do you have any software you can run to show what is sent and received?

    How would URLConnection get any cookies to send on its first usage?

    Here's what is sent by a simple program:

    That's sent by this code:
    Java Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
     
    public class GrabHTML {
     
     public static void Connect() throws Exception{
     
      //Set URL
      URL url = new URL("http://127.0.0.1:8080");   // Connect to my server
      URLConnection spoof = url.openConnection();
     
      //Spoof the connection so we look like a web browser
      spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
      BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
      String strLine = "";
     
      //Loop through every line in the source
      while ((strLine = in.readLine()) != null){
     
       //Prints each line to the console
       System.out.println(strLine);
      }
     
      System.out.println("End of page.");
     }
     
     public static void main(String[] args){
     
      try{
       //Calling the Connect method
       Connect();
      }catch(Exception e){
     
      }
     }
    }
    Well apparently you can use:

    Java Code:
    connection.setRequestProperty("Cookie", "cookiename=cookievalue");
    but that doesn't work for me. I just don't want to send or receive cookies at all.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    I just don't want to send or receive cookies at all.
    I don't know how you can keep the server from sending a Set-Cookie header field.
    You should be able NOT to send a Cookie header field.

  15. #15
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    I know for a fact that the server sends cookies, I got their names and values.

    How can I make it so my URLConnection doesn't save/send the cookies?

    I've been trying to figure this out forever, thanks.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    How can I make it so my URLConnection doesn't save/send the cookies?
    Does URLConnection save/send cookies without you telling it to?

  17. #17
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Does URLConnection save/send cookies without you telling it to?
    I believe so. But you're the pro here...

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Not really. I find out things by trial and error and by looking at the stash of programs I've written over the years.
    Why do you believe so? Do you have a small program that demonstrates it?

  19. #19
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Not really. I find out things by trial and error and by looking at the stash of programs I've written over the years.
    Why do you believe so? Do you have a small program that demonstrates it?
    According to the headers of the URLConnection, the site sends Set-Cookie requests.

    Also, I know it must be cookies because when I proxy the captcha still stays.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Yes, you wouldn't have any control over the site sending Set-Cookie.
    it must be cookies because when I proxy the captcha still stays
    No idea what causes the captcha to stay. Can there be other reasons?
    Can you show that your code sends Cookies?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 02-10-2011, 07:37 PM
  2. Java: Handling cookies when logging in with POST
    By cloakbot in forum New To Java
    Replies: 1
    Last Post: 06-17-2010, 12:19 AM
  3. Logging in with Java (cookies)
    By Supamagier in forum Advanced Java
    Replies: 0
    Last Post: 05-15-2009, 10:22 AM
  4. Deleting and Adding Lists in Java Applets
    By Bomber_Will in forum Java Applets
    Replies: 4
    Last Post: 11-28-2008, 06:52 AM
  5. JSP cookies example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •