使用httpclient4.3.X模擬post請求登陸網站獲取cookie資訊的操作
阿新 • • 發佈:2019-01-24
眾所周知,httpclient可以模擬登陸操作,下面我就來個例項測試一下,順便也回憶下這個開源工具的使用,直接上例子吧!
package com.pyc.search.crawler.node.login; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class LoginMis { private CloseableHttpClient httpclient; private HttpPost httppost;// 用於提交登陸資料 private HttpGet httpget;// 用於獲得登入後的頁面 private String login_success;// 用於構造上面的HttpGet public LoginMis() { httpclient = HttpClients.createDefault(); // mis登陸介面網址 httppost = new HttpPost("http://mis.pyc.com.cn/login.aspx"); } public void logIn(String name, String password) throws Exception { httppost.setHeader("Content-Type", "application/x-www-form-urlencoded"); String post ="__VIEWSTATE=%2FwEPDwUJNjUwNzE0MTM4ZGRq%2FuXaVF47TNHr5WWnDm%2F6haaRp0gOdDNIQoZp%2FJkyIA%3D%3D&__EVENTVALIDATION=%2FwEWBQLc1Oq3DgLEhISFCwKd%2B7qdDgKC3IeGDAK7q7GGCL0B2uWAzgYaqwKD%2F9eVKMfwK6lpSiVQr9EOO89rWz0w&txtName="+name+"&txtPwd="+password+"&btnLogin=%E7%99%BB+%E5%BD%95"; httppost.setEntity(new StringEntity(post, "utf-8")); try { // 提交登入資料 HttpResponse re = httpclient.execute(httppost); System.out.println("response: "+re); Header[] h =re.getAllHeaders(); for (Header header : h) { System.out.println(header.toString()); } // 獲得跳轉的網址 Header locationHeader = re.getFirstHeader("Location"); // 登陸不成功 if (locationHeader == null) { System.out.println("使用者 : "+name+"登陸不成功,請確認您的使用者名稱和密碼正確..."); System.out.println("使用者 : "+name+" \t密碼:"+password); return; } else// 成功 { login_success = "http://mis.pyc.com.cn"+locationHeader.getValue();// 獲取登陸成功之後跳轉連結 System.out.println("使用者: "+name+" 登陸成功,之後跳轉到的網頁網址:" + login_success); TimeUnit.SECONDS.sleep(3); System.out.println("使用者: "+name+" cookie資訊: "+re.getLastHeader("Set-Cookie").getValue()); TimeUnit.SECONDS.sleep(3); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void PrintText(String name) throws IOException { httpget = new HttpGet(login_success); HttpResponse re2 = null; try { re2 = httpclient.execute(httpget); // 輸出登入成功後的頁面 String str = EntityUtils.toString(re2.getEntity()); System.out.println("\n"+name+"首頁資訊如下:"); System.out.println(str.substring(8250,8400)); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { httppost.abort(); httpget.abort(); httpclient.close(); } } public static void main(String[] args) throws Exception { String name = "xxxxx", password = "xxxxxx"; // 自己的賬號,口令 LoginMis lr = new LoginMis(); lr.logIn(name, password); lr.PrintText(name); } }