import java.io.FileInputStream; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.io.IOUtils; public class Test { private static HttpClient httpClient = new HttpClient(); /* get请求 public static void main(String[] args) throws Exception { GetMethod getMethod = new GetMethod("http://commons.apache.org/downloads/download_logging.cgi"); httpClient.executeMethod(getMethod); InputStream responseBodyAsStream = getMethod.getResponseBodyAsStream(); String string = IOUtils.toString(responseBodyAsStream, "ISO-8859-1"); System.out.println(string); if(getMethod != null) { getMethod.releaseConnection(); } } */ /* post提交 */ public static void main2(String[] args) throws Exception { PostMethod postMethod = new PostMethod("http://popogame.netease.com/index.php"); NameValuePair[] pairs = new NameValuePair[]{ new NameValuePair("username" , "泡泡论坛账号"), new NameValuePair("password" , "泡泡论坛密码"), new NameValuePair("cookietime", "2592000"), new NameValuePair("loginfield" , "username"), new NameValuePair("questionid" , "0"), new NameValuePair("loginmode" ,""), new NameValuePair("answer" , ""), new NameValuePair("styleid" , "") , new NameValuePair("formhash" , "17a0ef64") }; postMethod.setRequestBody(pairs); httpClient.executeMethod(postMethod); String string = IOUtils.toString(postMethod.getResponseBodyAsStream(), "GB2312"); System.out.println(string); if(postMethod != null) { postMethod.releaseConnection(); } } /* 校内登陆加搜索人 可以无限制搜索 */ public static void main3(String[] args) throws Exception { PostMethod postMethod = new PostMethod("http://passport.renren.com/PLogin.do"); NameValuePair[] pairs = new NameValuePair[]{ new NameValuePair("email" , "[email protected]"), new NameValuePair("password" , "aaaaaa"), }; postMethod.setRequestBody(pairs); httpClient.executeMethod(postMethod); Header responseHeader = postMethod.getResponseHeader("location"); String url = responseHeader.getValue(); GetMethod getMethod = new GetMethod(url); httpClient.executeMethod(getMethod); if(postMethod != null) { postMethod.releaseConnection(); } if(getMethod != null) { getMethod.releaseConnection(); } Set<String> set = new LinkedHashSet<String>(); for (int i = 0; i <= 150; i = i + 10) { PostMethod searchMethod = new PostMethod("http://browse.renren.com/searchEx.do?from=opensearch"); searchMethod.setRequestBody(new NameValuePair[]{ new NameValuePair("ajax" , "1"), new NameValuePair("q" , "北京大学"), new NameValuePair("offset", i + "") }); httpClient.executeMethod(searchMethod); Pattern person = Pattern.compile("http://renren.com/profile.do\\?id=([\\d]+)"); String searchStr = IOUtils.toString(searchMethod.getResponseBodyAsStream(), "UTF-8"); searchStr = searchStr.replaceAll("\r", "").replaceAll("\n", ""); Matcher matcher = person.matcher(searchStr); while(matcher.find()) { String group = matcher.group(); System.out.println(group); set.add(group); } if(searchMethod != null) { searchMethod.releaseConnection(); } } System.out.println("-----------------------------------------------------------"); for (String string : set) { System.out.println(string); } } public static void connect() throws Exception { } @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); PostMethod postMethod = new PostMethod("http://passport.renren.com/PLogin.do"); postMethod.setRequestHeader("User-Agent", "Firefox Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3"); NameValuePair[] pairs = new NameValuePair[]{ new NameValuePair("email" , "校内账号"), new NameValuePair("password" , "校内密码"), }; postMethod.setRequestBody(pairs); httpClient.executeMethod(postMethod); Header responseHeader = postMethod.getResponseHeader("location"); String url = responseHeader.getValue(); GetMethod getMethod1 = new GetMethod(url); httpClient.executeMethod(getMethod1); if(getMethod1 != null) { getMethod1.releaseConnection(); } if(postMethod != null) { postMethod.releaseConnection(); } List<String> readLines = IOUtils.readLines(new FileInputStream("c:\\a.txt") , "gb2312"); int count = 0; for (String string : readLines) { count ++; GetMethod getMethod = new GetMethod(string); getMethod.setRequestHeader("User-Agent", "Firefox Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3"); httpClient.executeMethod(getMethod); if(count == 110) { String string2 = IOUtils.toString(getMethod.getResponseBodyAsStream(), "UTF-8"); System.out.println(string2); } if(getMethod != null) { getMethod.releaseConnection(); } } } }