天天看點

Java通過httpclient擷取cookie模拟登入(網絡爬蟲/HTTP)

package

Step1;

import

org.apache.commons.httpclient.Cookie;

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;

public

class

HttpLogin {

public

static

void

main(String[] args) {

// 登陸 Url

String loginUrl = 

"http://passport.mop.com/?targetUrl=http://hi.mop.com/?&g=1447141423230&loginCheck=UNLOGINED"

;

// 需登陸後通路的 Url

String dataUrl = 

"http://hi.mop.com/?"

;

HttpClient httpClient = 

new

HttpClient();

// 模拟登陸,按實際伺服器端要求選用 Post 或 Get 請求方式

PostMethod postMethod = 

new

PostMethod(loginUrl);

// 設定登陸時要求的資訊,使用者名和密碼

NameValuePair[] data = { 

new

NameValuePair(

"loginName"

"chzeze123"

), 

new

NameValuePair(

"loginPasswd"

"**"

) };

postMethod.setRequestBody(data);

try

{

// 設定 HttpClient 接收 Cookie,用與浏覽器一樣的政策

httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

int

statusCode=httpClient.executeMethod(postMethod);

// 獲得登陸後的 Cookie

Cookie[] cookies = httpClient.getState().getCookies();

StringBuffer tmpcookies = 

new

StringBuffer();

for

(Cookie c : cookies) {

tmpcookies.append(c.toString() + 

";"

);

System.out.println(

"cookies = "

+c.toString());

}

if

(statusCode==

302

){

//重定向到新的URL

System.out.println(

"模拟登入成功"

);

// 進行登陸後的操作

GetMethod getMethod = 

new

GetMethod(dataUrl);

// 每次通路需授權的網址時需帶上前面的 cookie 作為通行證

getMethod.setRequestHeader(

"cookie"

, tmpcookies.toString());

// 你還可以通過 PostMethod/GetMethod 設定更多的請求後資料

// 例如,referer 從哪裡來的,UA 像搜尋引擎都會表名自己是誰,無良搜尋引擎除外

postMethod.setRequestHeader(

"Referer"

"http://passport.mop.com/"

);

postMethod.setRequestHeader(

"User-Agent"

"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"

);

httpClient.executeMethod(getMethod);

// 列印出傳回資料,檢驗一下是否成功

String text = getMethod.getResponseBodyAsString();

System.out.println(text);

}

else

{

System.out.println(

"登入失敗"

);

}

}

catch

(Exception e) {

e.printStackTrace();

}

}

}