用Java模拟浏览器操作
1 package sky.dong.test; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import org.apache.commons.httpclient.Cookie; 6 import org.apache.commons.httpclient.Header; 7 import org.apache.commons.httpclient.HttpClient; 8 import org.apache.commons.httpclient.NameValuePair; 9 import org.apache.commons.httpclient.cookie.CookiePolicy; 10 import org.apache.commons.httpclient.methods.PostMethod; 11 import org.apache.commons.httpclient.params.HttpMethodParams; 12 13 public class HttpLoginTest { 14 15 public static void main(String[] args) { 16 String url = 17 "http://discuzdemo.c88.53dns.com/logging.php?action=login&loginsubmit=yes&floatlogin=yes"; //论坛的登陆页面 18 String url2 = 19 "http://discuzdemo.c88.53dns.com/post.php?infloat=yes&action=newthread&fid=2&extra=&topicsubmit=yes&inajax=1"; //论坛的发贴页面 20 HttpClient httpClient = new HttpClient(); 21 //httpClient.getHostConfiguration().setProxy("222.247.62.195", 8080); 22 httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); 23 PostMethod postMethod = new PostMethod(url); 24 PostMethod postMethod2 = new PostMethod(url2); 25 NameValuePair[] data = { 26 new NameValuePair("username", "123"), 27 new NameValuePair("referer", "http://discuzdemo.c88.53dns.com/index.php"), 28 new NameValuePair("password", "123"), 29 new NameValuePair("loginfield", "username"), 30 new NameValuePair("questionid", "0"), 31 new NameValuePair("formhash", "fc922ca7"), 32 }; 33 postMethod.setRequestHeader( 34 "Referer", 35 "http://discuzdemo.c88.53dns.com/index.php" 36 ); 37 postMethod.setRequestHeader("Host", "discuzdemo.c88.53dns.com"); 38 // postMethod.setRequestHeader("Connection", "keep-alive"); 39 // postMethod.setRequestHeader("Cookie", "jbu_oldtopics=D123D; 40 // jbu_fid2=1249912623; smile=1D1; jbu_onlineusernum=2; 41 // jbu_sid=amveZM"); 42 postMethod.setRequestHeader( 43 "User-Agent", 44 "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2" 45 ); 46 postMethod.setRequestHeader( 47 "Accept", 48 "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 49 ); 50 // postMethod.setRequestHeader("Accept-Encoding", "gzip,deflate"); 51 // postMethod.setRequestHeader("Accept-Language", "zh-cn"); 52 // postMethod.setRequestHeader("Accept-Charset", 53 // "GB2312,utf-8;q=0.7,*;q=0.7"); 54 postMethod.setRequestBody(data); 55 try { 56 httpClient.executeMethod(postMethod); 57 StringBuffer response = new StringBuffer(); 58 BufferedReader reader = new BufferedReader( 59 new InputStreamReader(postMethod.getResponseBodyAsStream(), "gb2312") 60 ); //以gb2312编码方式打印从服务器端返回的请求 61 String line; 62 while ((line = reader.readLine()) != null) { 63 response.append(line).append(System.getProperty("line.separator")); 64 } 65 reader.close(); 66 Header header = postMethod.getResponseHeader("Set-Cookie"); 67 Cookie[] cookies = httpClient.getState().getCookies(); //取出登陆成功后,服务器返回的cookies信息,里面保存了服务器端给的“临时证” 68 String tmpcookies = ""; 69 for (Cookie c : cookies) { 70 tmpcookies = tmpcookies + c.toString() + ";"; 71 System.out.println(c); 72 } 73 System.out.println(tmpcookies); 74 // System.out.println(header.getValue()); 75 System.out.println(response); 76 NameValuePair[] data2 = { 77 new NameValuePair("subject", "测试自动发贴"), 78 new NameValuePair("message", "能否发贴成功呢?测试一下就知道了"), 79 new NameValuePair("updateswfattach", "0"), 80 new NameValuePair("wysiwyg", "0"), 81 new NameValuePair("checkbox", "0"), 82 new NameValuePair("handlekey", "newthread"), 83 new NameValuePair("formhash", "885493ec"), 84 }; 85 postMethod2.setRequestHeader("cookie", tmpcookies); //将“临时证明”放入下一次的发贴请求操作中 86 postMethod2 87 .getParams() 88 .setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk"); //因为发贴时候有中文,设置一下请求编码 89 postMethod2.setRequestHeader( 90 "Referer", 91 "http://discuzdemo.c88.53dns.com/forumdisplay.php?fid=4" 92 ); 93 postMethod2.setRequestHeader("Host", "discuzdemo.c88.53dns.com"); 94 // postMethod.setRequestHeader("Connection", "keep-alive"); 95 // postMethod.setRequestHeader("Cookie", "jbu_oldtopics=D123D; 96 // jbu_fid2=1249912623; smile=1D1; jbu_onlineusernum=2; 97 // jbu_sid=amveZM"); 98 postMethod2.setRequestHeader( 99 "User-Agent", 100 "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2" 101 ); 102 postMethod2.setRequestHeader( 103 "Accept", 104 "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 105 ); //以上操作是模拟浏览器的操作,使用服务器混淆 106 postMethod2.setRequestBody(data2); 107 httpClient.executeMethod(postMethod2); 108 StringBuffer response1 = new StringBuffer(); 109 BufferedReader reader1 = new BufferedReader( 110 new InputStreamReader(postMethod2.getResponseBodyAsStream(), "gb2312") 111 ); 112 String line1; 113 while ((line1 = reader1.readLine()) != null) { 114 response1.append(line1).append(System.getProperty("line.separator")); 115 } 116 reader1.close(); 117 System.out.println(response1); 118 } catch (Exception e) { 119 System.out.println(e.getMessage()); 120 // TODO: handle exception 121 } finally { 122 postMethod.releaseConnection(); 123 postMethod2.releaseConnection(); 124 } 125 } 126 }
记录有用的信息和数据,并分享!