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

P

Phil Powell

URL: http://valsignalandet.com/cookiegrab.php?name=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>
UlXMIxtEQXX1jVt5
</body>
</html>

The "U1XmIxtEQXX1jVt5" is never seen were you to do a
System.out.println. 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.openConnection();
conn.setDoInput(true);
conn.setDoOutput(false);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Content-type", "text/html");
BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
while ((stuff = in.readLine()) != null) {
System.out.println("stuff = " + stuff);
cookieText += stuff;
}
in.close();
in = null;
conn = null; url = null;
} catch (Exception e) {
System.out.println("Could not connect to " + this.url + "?name=" +
this.cookieName);
e.printStackTrace();
} finally {
//cookieText = cookieText.replaceAll("[\\n\\r\\s\\t]+", "");
//cookieText = cookieText.replaceAll("<[^>]+>", "");
}
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top