I'm not doing anything malicious the forum in the example is just to test logging into a server without opening the browser. I edited a lot of the urls out too.
Code
import java.io.BufferedReader;import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.*;
public class WorkingAOPSLogin {
private static URL urlObj;
private static URLConnection connect;
public static void main(String[]args){
try {
urlObj = new URL("somewebsite/Forum/ucp.php?mode=login&redirect=/Forum/index.php");
} catch (MalformedURLException e) {e.printStackTrace();}
try {
connect = urlObj.openConnection();
connect.setDoOutput(true);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
writer.write("username=MYUSERNAME&password=MYPASSWORD&login=Login");
/*connect = (new URL("somewebsite/Forum/posting.php?mode=post&f=224")).openConnection();
connect.setDoOutput(true);
writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
writer.write("subject=hello&post=Submit");
*/
writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String lineRead = "";
while((lineRead = reader.readLine()) != null){
System.out.println(lineRead);
}
reader.close();
} catch (IOException e) {e.printStackTrace();}
System.out.println("\n\n here \n\n");
String headerName;
for (int i=1; (headerName = connect.getHeaderFieldKey(i)) != null; i++) {
if(headerName.equals("Set-Cookie")){
String cookie = connect.getHeaderField(i);
System.out.println(cookie);
}
}
}
}
When I run this code I get the following output:
Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3site/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="w3site/1999/xhtml" dir="ltr" lang="en-gb" xml:lang="en-gb">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en-gb" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="imagetoolbar" content="no" />
<meta name="resource-type" content="document" />
<meta name="distribution" content="global" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="viewport" content="width=device-width,height=device-height,user-scalable=yes" />
<meta http-equiv="refresh" content="3;url=website/Forum/index.php?" />
<title>Art of Problem Solving • User Control Panel • Welcome to Art of Problem Solving</title>
<script type="text/javascript" src="googleapis/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript" src="/system/js/aops.js?r=4"></script>
<script type="text/javascript" src="./styles/aops/template/forum_fn.js"></script>
<script type="text/javascript">
var HOST = '';
var USERNAME = '';
var USER_ID = 1;
</script>
</head>
<body class="ltr">
<div style="width: 100%;background-color: #e1e8f5;border-bottom:1px solid #bbb;height:28px;margin: 0;">
<div style="padding:5px 10px">
<div style="float:right;line-height:18px;">
</div>
<div style="float:left;font-size:14px;">
Art of Problem Solving
</div>
</div>
</div>
<div id="main">
<div style="text-align:center;margin-top:30px;">
<h1>Welcome to Art of Problem Solving</h1>
</div>
</div>
</body>
</html>
here
PHPSESSID=hvi9ivjsiek8qoijiagskmqg4sirv8au; path=/
phpbb3_3oerp_u=1; expires=Tue, 09-Jul-2013 18:23:01 GMT; path=/; domain=.website; HttpOnly
phpbb3_3oerp_k=; expires=Tue, 09-Jul-2013 18:23:01 GMT; path=/; domain=.website; HttpOnly
phpbb3_3oerp_sid=714e8054b89f05c75d43658b84e1e843; expires=Tue, 09-Jul-2013 18:23:01 GMT; path=/; domain=.website; HttpOnly
phpbb3_3oerp_hidden=; expires=Sun, 09-Jun-2013 15:59:41 GMT; path=/; domain=.website; HttpOnly
phpbb3_3oerp_collapsed=; expires=Sun, 09-Jun-2013 15:59:41 GMT; path=/; domain=.website; HttpOnly
__umtu=a%3A1%3A%7Bi%3A0%3Bs%3A9%3A%MYUSERNAME%22%3B%7D; expires=Mon, 09-Jun-2014 18:23:01 GMT; path=/; domain=.website; httponly
phpbb3_3oerp_u=74281; expires=Tue, 09-Jul-2013 18:23:01 GMT; path=/; domain=.website; HttpOnly
phpbb3_3oerp_k=; expires=Tue, 09-Jul-2013 18:23:01 GMT; path=/; domain=.website; HttpOnly
phpbb3_3oerp_sid=8122fba012d8807aa2bbd976609ec3f7; expires=Tue, 09-Jul-2013 18:23:01 GMT; path=/; domain=.website; HttpOnly
And I take "You have been successfully logged in." to be a good sign. However, when I try to do the same thing purely using sockets (without the java URLConnection class which also uses sockets but is more limiting) I can't seem to make it work. Here's my code for that:
Code
//java socket client exampleimport java.io.*;
import javanet.*;
public class SocketTest
{
public static void main(String[] args) throws IOException
{
Socket s = new Socket();
String host = "www website com";
PrintWriter s_out = null;
BufferedReader s_in = null;
try
{
s.connect(new InetSocketAddress(host , 80));
System.out.println("Connected");
//writer for socket
s_out = new PrintWriter( s.getOutputStream(), true);
//reader for socket
s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
//Host not found
catch (UnknownHostException e)
{
System.err.println("Don't know about host : " + host);
System.exit(1);
}
//Send message to server
s_out.println("GET /Forum/ucp.php?mode=login&username=MYUSERNAME&password=MYPASSWORD&login=Login&redirect=/Forum/ucp.php?mode=logout HTTP/1.1\r\nHost: www website com\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
System.out.println("Message send");
//Get response from server
String response;
while ((response = s_in.readLine()) != null)
{
System.out.println( response );
}
//close the i/o streams
s_out.close();
s_in.close();
//close the socket
s.close();
System.out.println("Program Done");
}
}
This code gives the following output:
Code
Connected
Message send
HTTP/1.1 302 Found
Date: Sun, 09 Jun 2013 18:26:28 GMT
Server: ApacheSet-Cookie: PHPSESSID=s05jir45nq8rtlairradv2054si0vuns; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
location: www website com/Forum/ucp.php?mode=login&username=MYUSERNAME&password=MYPASSWORD&login=Login&redirect=/Forum/ucp.php?mode=logout
Vary: Accept-Encoding,User-Agent
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Program Done
It's giving a 302 Found error for some reason? When I run my Live HTTP Headers tool and I login I get the following:
Code
website/Forum/ucp.php?mode=login
POST /Forum/ucp.php?mode=login HTTP/1.1
Host: website
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: www website com/Forum/ucp.php?mode=login&redirect=/Forum/ucp.php%3fmode=logout
Cookie: PHPSESSID=j7neuj0r2imvtnbhmj9pv92tona0mg09; __utma=119266009.1728699164.1370785148.1370794501.1370802682.4; __utmc=119266009; __utmz=119266009.1370802682.4.3.utmccn=(organic)|utmcsr=google|utmctr=|utmcmd=organic; __umtu=a%3A1%3A%7Bi%3A0%3Bs%3A9%3A%22MYUSERNAME%22%3B%7D; __utmb=119266009; phpbb3_3oerp_u=1; phpbb3_3oerp_k=; phpbb3_3oerp_sid=0d14419aef16b5096f1d3c22b18fe236
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 129
username=MYUSERNAME&password=MYPASSWORD&login=Login&sid=0d14419aef16b5096f1d3c22b18fe236&redirect=%2FForum%2Fucp.php%3Fmode%3Dlogout
Shouldn't I be able to copy the exact GET/POST message that Live HTTP Headers gives for any request and send that through the buffered writer? (and I get the same output when I change the GET message to POST). Sorry this post is so long but thanks for reading if you did!
This post was edited by layhooo3 on Jun 9 2013 01:55pm