import java.net.*; import java.io.*; import java.util.*; public class PostTest { public PostTest() { } public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.println("Username? "); String username = scan.nextLine(); System.out.println("Password? "); String pw = scan.nextLine(); try { // Construct data String data = URLEncoder.encode("uid", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8"); data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(pw, "UTF-8"); // Create a socket to the host String hostname = "vuauth.villanova.edu"; int port = 80; InetAddress addr = InetAddress.getByName(hostname); Socket socket = new Socket(addr, port); // Send header String path = "/vusecurity/authentication"; BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")); wr.write("POST "+path+" HTTP/1.1\r\n"); wr.write("Host: vuauth.villanova.edu\r\n"); wr.write("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11\r\n"); wr.write("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"); wr.write("Accept-Language: en-us,en;q=0.5\r\n"); wr.write("Accept-Encoding: gzip,deflate\r\n"); wr.write("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"); wr.write("Keep-Alive: 300\r\n"); wr.write("Connection: keep-alive\r\n"); wr.write("Referer: http://www.villanova.edu/homepage/index.htm\r\n"); wr.write("Content-Type: application/x-www-form-urlencoded\r\n"); wr.write("Content-Length: "+data.length()+"\r\n"); wr.write("\r\n"); // Send data wr.write(data); wr.flush(); // Get response BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; String cookies = ""; int endIndex; while ((line = rd.readLine()) != null) { //System.out.println(line); if (line.length() > 11 && line.substring(0,11).equals("Set-Cookie:")) { endIndex = line.indexOf(";"); if (cookies != "") cookies += ";" + line.substring(11, endIndex); else cookies = line.substring(11, endIndex); } } wr.close(); rd.close(); //send get request // Create a socket to the host hostname = "www.villanova.edu"; port = 80; addr = InetAddress.getByName(hostname); socket = new Socket(addr, port); // Send header path = "/homepage/index.htm"; wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")); wr.write("GET /homepage/index.htm HTTP/1.1\r\n"); wr.write("Host: www.villanova.edu\r\n"); wr.write("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11\r\n"); wr.write("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"); wr.write("Accept-Language: en-us,en;q=0.5\r\n"); wr.write("Accept-Encoding: gzip,deflate\r\n"); wr.write("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"); wr.write("Keep-Alive: 300\r\n"); wr.write("Connection: keep-alive\r\n"); wr.write("Referer: http://www.villanova.edu/homepage/index.htm\r\n"); wr.write("Cookie:" + cookies + "\r\n"); wr.write("\r\n"); // Send data wr.write(data); wr.flush(); // Get response rd = new BufferedReader(new InputStreamReader(socket.getInputStream())); String result = ""; boolean start = false; while ((line = rd.readLine()) != null) { if (line.length() > 23 && line.substring(0,23).equals("