By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

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

P: n/a

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:


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


* 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) { //
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.setRequestProperty("Content-type", "text/html");
BufferedReader in = new BufferedReader(new
while ((stuff = in.readLine()) != null) {
System.out.println("stuff = " + stuff);
cookieText += stuff;
in = null;
conn = null; url = null;
} catch (Exception e) {
System.out.println("Could not connect to " + this.url + "?name=" +
} 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.

Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.