I have set up a login area on my PHP server. The members.php file requires login; after I login a session is created. The session lasts for a while. I want to make this work so that while the session is still valid the java applet should be able to access members.php page.
I have embedded the Java applet into the members.php page. It makes a HttpURLConnection request, however when I get the response I find that it was redirected by the PHP server to the login page.
How do I set this up correctly?
Here is the Java Applet code:
import java.applet.Applet;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class phpConnectApplet extends Applet {
private static final long serialVersionUID = 1L;
public void init() {
URL url = null;
try {
url = new URL("http://www.example.com/members.php");
URLConnection urlConn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setUseCaches(false);
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream output = new DataOutputStream(httpConn.getOutputStream());
String content = "action=blah"; //just to test the PHP file
output.writeBytes(content);
output.flush();
output.close();
DataInputStream in = new DataInputStream(urlConn.getInputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(in));
String str, result = "";
while ((str = input.readLine()) != null) {
result = result + str + "\n";
}
input.close();
Map<String, List<String>> headers = httpConn.getHeaderFields();
List<String> values = headers.get("Set-Cookie");
String cookieValue = null;
for (String v:values) {
if (cookieValue == null)
cookieValue = v;
else
cookieValue = cookieValue + ";" + v;
}
System.out.println(cookieValue);
JFrame f = new JFrame("App Title");
f.add(new JTextArea(result));
f.setMaximumSize(new Dimension(400,300));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
} catch (MalformedURLException me) {
me.printStackTrace();
} catch(IOException ie) {
ie.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
The output for this is a JFrame with one JTextField which contains the output HTML of the login page.
You need to capture the value of your PHPSESSID cookie (or whatever you are using for the cookie name) and add it to the request in the HttpURLConnection. This MAY come through as a system parameter, but if not, you can embed the session ID as an applet attribute on the page launching the applet. (I haven’t experimented with this part specifically)
Here’s a tutorial that explains how to send cookies in the URLConnection class: http://www.hccp.org/java-net-cookie-how-to.html
Specifically see the section titled Setting a cookie value in a request.