1 package test001; 2 3 import java.io.BufferedReader; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.io.OutputStreamWriter; 9 import java.net.HttpURLConnection; 10 import java.net.URL; 11 12 import org.apache.commons.io.IOUtils; 13 14 public class testhttpx { 15 public static void testPost01() throws IOException { 16 String surl = "http://login.goodjobs.cn/index.php/action/UserLogin"; 17 /** 18 * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using 19 * java.net.URL and //java.net.URLConnection 20 */ 21 URL url = new URL(surl); 22 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 23 24 /** 25 * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 26 * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: 27 */ 28 connection.setDoOutput(true); 29 /** 30 * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... 31 */ 32 OutputStreamWriter out = new OutputStreamWriter(connection 33 .getOutputStream(), "GBK"); 34 //其中的memberName和password也是阅读html代码得知的,即为表单中对应的参数名称 35 out.write("memberName=myMemberName&password=myPassword"); // post的关键所在! 36 // remember to clean up 37 out.flush(); 38 out.close(); 39 40 // 取得cookie,相当于记录了身份,供下次访问时使用 41 String cookieVal = connection.getHeaderField("Set-Cookie"); 42 String s = "http://user.goodjobs.cn/dispatcher.php/module/Resume/action/Preview"; 43 //重新打开一个连接 44 url = new URL(s); 45 HttpURLConnection resumeConnection = (HttpURLConnection) url 46 .openConnection(); 47 if (cookieVal != null) { 48 //发送cookie信息上去,以表明自己的身份,否则会被认为没有权限 49 resumeConnection.setRequestProperty("Cookie", cookieVal); 50 } 51 resumeConnection.connect(); 52 InputStream urlStream = resumeConnection.getInputStream(); 53 BufferedReader bufferedReader = new BufferedReader( 54 new InputStreamReader(urlStream)); 55 String ss = null; 56 String total = ""; 57 while ((ss = bufferedReader.readLine()) != null) { 58 total += ss; 59 } 60 IOUtils.write(total, new FileOutputStream("d:/index.html")); 61 bufferedReader.close(); 62 } 63 64 public static void main(String[] args) throws IOException { 65 66 testPost01(); 67 68 } 69 }
1 package test001; 2 3 import java.io.BufferedReader; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.io.OutputStreamWriter; 9 import java.net.HttpURLConnection; 10 import java.net.URL; 11 12 import org.apache.commons.io.IOUtils; 13 14 public class testhttps { 15 public static void testPost02() throws IOException { 16 String surl = "http://www.dekevin.com/wp-login.php"; 17 /** 18 * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using 19 * java.net.URL and //java.net.URLConnection 20 */ 21 URL url = new URL(surl); 22 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 23 24 /** 25 * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 26 * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: 27 */ 28 connection.setDoOutput(true); 29 /** 30 * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... 31 */ 32 OutputStreamWriter out = new OutputStreamWriter(connection 33 .getOutputStream(), "GBK"); 34 //其中的memberName和password也是阅读html代码得知的,即为表单中对应的参数名称 35 out.write("log=dekevin&pwd=luowende103807"); // post的关键所在! 36 // remember to clean up 37 out.flush(); 38 out.close(); 39 40 // 取得cookie,相当于记录了身份,供下次访问时使用 41 String cookieVal = connection.getHeaderField("Set-Cookie"); 42 String s = "http://www.dekevin.com/wp-admin/"; 43 //重新打开一个连接 44 url = new URL(s); 45 HttpURLConnection resumeConnection = (HttpURLConnection) url 46 .openConnection(); 47 if (cookieVal != null) { 48 //发送cookie信息上去,以表明自己的身份,否则会被认为没有权限 49 resumeConnection.setRequestProperty("Cookie", cookieVal); 50 } 51 resumeConnection.connect(); 52 InputStream urlStream = resumeConnection.getInputStream(); 53 BufferedReader bufferedReader = new BufferedReader( 54 new InputStreamReader(urlStream)); 55 String ss = null; 56 String total = ""; 57 while ((ss = bufferedReader.readLine()) != null) { 58 total += ss; 59 } 60 IOUtils.write(total, new FileOutputStream("d:/index.html")); 61 bufferedReader.close(); 62 } 63 64 public static void main(String[] args) throws IOException { 65 66 testPost02(); 67 68 } 69 70 }