Java class needs to get Cookie from PHP/Javascript URL page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    Java class needs to get Cookie from PHP/Javascript URL page

    URL: http://valsignalandet.com/cookiegrab...=valIdentifier

    This page produces a cookie value and sends it back as HTML using PHP
    and Javascript to obtain it. This URL will be used as the value for a
    URLConnection object in a Java class to obtain the results of this
    page to get, of all things, a cookie value.

    Simply put: it's the ONLY way I know of where I can use Java to
    ultimately obtain a cookie since this is an application class (NOT a
    servlet) that has to obtain a cookie from your own machine and
    retrieve its value.

    The class itself works except that it obtains all of the HTML tags and
    NOT the body content itself:

    <html>
    <head>
    <title></title>
    </head>
    <body>
    UlXMIxtEQXX1jVt 5
    </body>
    </html>

    The "U1XmIxtEQXX1jV t5" is never seen were you to do a
    System.out.prin tln. For the Java people reading this, here is the
    entire class:

    /**
    * @version JDK 1.4
    * @author Phil Powell
    */

    import java.io.*;
    import java.net.*;

    /**
    * This class will set and get Cookies from remote URL script.
    */
    public class CookieRetriever {

    //---------------------- --* PROPERTIES *--
    -------------------------------

    private String url = null; // PRIVATE STRING PROPERTY
    private String cookieName = null; // PRIVATE STRING PROPERTY

    //---------------------- --* END OF PROPERTIES *--
    ------------------------

    /**
    * @param url URL of page that will have cookie in HTTP headers
    * @param cookieName name of cookie
    */
    public CookieRetriever (String url, String cookieName) { //
    CONSTRUCTOR
    this.url = url;
    this.cookieName = cookieName;
    }


    //---------------------- --* GETTER/SETTER METHODS *--
    ---------------------

    /**
    * @return String name of cookie
    *
    * To use this method you will have to be sure to have your URL pass
    the name
    * of the cookie and not necessarily the cookie object itself
    */
    public String getCookieVal() {
    String stuff = null, cookieText = "";
    try {
    URL url = new URL(this.url + "?name=" + this.cookieName );
    URLConnection conn = url.openConnect ion();
    conn.setDoInput (true);
    conn.setDoOutpu t(false);
    conn.setUseCach es(false);
    conn.setDefault UseCaches(false );
    conn.setRequest Property("Conte nt-type", "text/html");
    BufferedReader in = new BufferedReader( new
    InputStreamRead er(conn.getInpu tStream()));
    while ((stuff = in.readLine()) != null) {
    System.out.prin tln("stuff = " + stuff);
    cookieText += stuff;
    }
    in.close();
    in = null;
    conn = null; url = null;
    } catch (Exception e) {
    System.out.prin tln("Could not connect to " + this.url + "?name=" +
    this.cookieName );
    e.printStackTra ce();
    } finally {
    //cookieText = cookieText.repl aceAll("[\\n\\r\\s\\t]+", "");
    //cookieText = cookieText.repl aceAll("<[^>]+>", "");
    }
    return cookieText.trim ();
    }

    //-------------------------- --* END OF GETTER/SETTER METHODS *--
    ----------------------------


    }

    Again the task is extremely simple: A Java class that obtains a cookie
    value. The only way I know how: Write a Java class wrapper that
    instantiates a URLConnection object to a PHP script I write that
    ITSELF (using Javascript as well) obtains the cookie value and returns
    the HTML resultset back to the Java class method.

    Phil
Working...