httpclient模擬登陸java
package com.zhaowu.renwu3; import java.io.IOException; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; 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.httpclient.params.HttpMethodParams; public class BoKeYuanLogin { public static void main(String[] args) { String username = "******"; String password = "******"; String loginUrl = "https://passport.cnblogs.com/login.aspx"; String targetUrl = "https://www.cnblogs.com/"; HttpClientLogin(username, password, loginUrl, targetUrl); } private static void HttpClientLogin(String username, String password, String loginUrl, String targetUrl) { // 建立一個客戶端,相似於打開一個瀏覽器 HttpClient httpClient = new HttpClient(); httpClient.getParams().setParameter(HttpMethodParams.HTTP_URI_CHARSET, "utf-8"); // 建立post方法實例,模擬登陸 PostMethod postMethod = new PostMethod(loginUrl); // 設置登錄時要求的信息,即填入各個表單域的值,用戶名和密碼 NameValuePair[] data = { new NameValuePair("username", username),
new NameValuePair("password", password) }; // 將表單的值放入到post方法中 postMethod.setRequestBody(data); try { // 設置 HttpClient 接收 Cookie,用與瀏覽器同樣的策略 httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // 得到響應狀態碼 int postStatusCode = httpClient.executeMethod(postMethod); System.out.println("postStatusCode: " + postStatusCode); System.out.println("======================1=================="); // 得到登錄後的 Cookie Cookie[] cookies = httpClient.getState().getCookies(); System.out.println("cookies: " + cookies.toString()); System.out.println("======================2=================="); StringBuffer sb = new StringBuffer(); for (Cookie cookie : cookies) { sb.append(cookie.toString() + ";"); System.out.println(cookie.getName() + ":" + cookie.getValue()); System.out.println("-------------------------"); } /*System.out.println(sb.toString()); System.out.println("======================5==================");*/ GetMethod getMethod = new GetMethod(targetUrl); getMethod.setRequestHeader("Cookie", sb.toString()); getMethod.setRequestHeader("Host", "passport.cnblogs.com"); getMethod.setRequestHeader("Referer", "https://www.cnblogs.com/"); getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"); int getStatusCode = httpClient.executeMethod(getMethod); System.out.println("getStatusCode: " + getStatusCode); System.out.println("======================3=================="); // 打印出返回數據,檢驗一下是否成功 String content = getMethod.getResponseBodyAsString(); System.out.println(content); System.out.println("======================4=================="); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }