java的httpclient_使用java HttpClient 与Web服务器交互 - elliott - 博客园

下面程序想自动登陆系统:

public static void main(String[] args) throws Exception{

HttpClient client = new HttpClient();

client.getHostConfiguration().setHost("127.0.0.1", 80,"http");

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

PostMethod post = new PostMethod("/login/login.asp");

NameValuePair name = new NameValuePair("loginname",

"kingseo@163.com");

NameValuePair pass = new NameValuePair("password", "123456");

NameValuePair int_count = new NameValuePair("int_count", "999");

NameValuePair bkurl = new NameValuePair("bkurl=", "");

post.setRequestHeader("Referer",

"http://127.0.0.1/login/login.asp");

post.setRequestHeader("ContentType",

"application/x-www-form-urlencoded");

post.setRequestHeader("Content-Length", "62");

post.setRequestHeader("Cookie",

"JSsUserInfo=382C3D7551694479056D1D79446C58754A345C2C4E755D694E79786D6279486C02751034032C1E751F6910795B6D6C79446C5F754A34432C1A75036918790F6D79793B6C54754234232C377551694E79606D6179486C52753C34202C447507691C795A6D4B79066C0C7516342D2C48755A6946791A6D4F791A6C047542343E2C2D75516944790F6D6D79216C54754C34402C487555695579056D19794F6C5B754834562C3A7528694879046D1779206C3D7544345C2C427539693479096D6679276C587548345C2C48755F694479036D1D79436C52753834212C44755C694E798;");

post.setRequestBody(new

NameValuePair[]{name,pass,int_count,bkurl});

int statuscode = client.executeMethod(post);

if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)  ¦ ¦

(statuscode == HttpStatus.SC_MOVED_PERMANENTLY)  ¦ ¦

(statuscode == HttpStatus.SC_SEE_OTHER)  ¦ ¦

(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)){

Header header = post.getResponseHeader("location");

if (header != null) {

String newurl = header.getValue();

if ((newurl == null)  ¦ ¦ (newurl.equals("")))

newurl = "/";

GetMethod get = new GetMethod(newurl);

client.executeMethod(get);

BufferedReader bf = new BufferedReader(new

InputStreamReader(get.getResponseBodyAsStream()));

String s = null;

while ((s=bf.readLine()) != null){

System.out.println(s);

}

get.releaseConnection();

} else{

System.out.println("Invalid redirect");

}

post.releaseConnection();

}else{

System.out.println("ok");

}

}

通过ieHTTPHeaders看到以下信息:

POST /login/login.asp?DYWE=1210926948718.119132.1211187954.1211188187.8

HTTP/1.1

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,

application/x-shockwave-flash, application/vnd.ms-excel,

application/vnd.ms-powerpoint, application/msword, */*

Referer: http://127.0.0.1/login/login.asp

Accept-Language: zh-cn

Content-Type: application/x-www-form-urlencoded

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)

Host: 127.0.0.1

Content-Length: 62

Connection: Keep-Alive

Cache-Control: no-cache

Cookie: strloginusertype=4;

__zpWAM=1210926948718.119132.1211187954.1211188187.8;

firstchannelurl=http%3A//127.0.0.1/login/login.asp%3FBkUrl%3D%252Fmyzhaopin%252Fresume%255Fnav%252Easp%253FDYWE%253D1210926948718%252E119132%252E1210927448%252E1211158566%252E3%2526firstRef%253D%252D;

lastchannelurl=; JSShowname=kingseo%40163%2Ecom;

JSloginnamecookieindex=kingseo%40163%2Ecom; myzl111113171=0;

__zpWAMs1=1; __zpWAMs2=1

loginname=kingseo@163.com&password=123456&int_count=999&bkurl=

HTTP/1.0 302 Moved Temporarily

Date: Mon, 19 May 2008 09:14:22 GMT

Server: Microsoft-IIS/6.0

X-Powered-By: ASP.NET

Location: /person/resume_index.asp?t=62062.7

Content-Length: 158

Content-Type: text/html; Charset=utf-8

Expires: Mon, 19 May 2008 09:14:22 GMT

Set-Cookie: JSpUserInfo=; domain=127.0.0.1; path=/

Set-Cookie:

JSsUserInfo=342C713654365A611D350769076B502C063658365C611D350D697A6B2F2C08360236026142355169456B042C5A3629365A611A350569186B022C5A3604365061793578690B6B5A2C7B3627365661173562697B6B5C2C0E362C36266111355D695F6B0F2C52361A360E6143357669076B572C06364736086143355B690D6B322C613654365A6117357769626B5C2C003644365A6115351669076B562C0F3658365F6117357569726B5C2C053652363E6178350B69076B5A2C60362836566166356469076B502C0436583658611D350169076B572C0E3628362761113506690D6B9;

domain=127.0.0.1; path=/

Set-Cookie: JSShowname=kingseo%40163%2Ecom; expires=Tue, 31-Dec-2019

16:00:00 GMT; domain=127.0.0.1; path=/

Set-Cookie: JSloginnamecookieindex=kingseo%40163%2Ecom; expires=Tue,

31-Dec-2019 16:00:00 GMT; domain=127.0.0.1; path=/

Set-Cookie: strloginusertype=1; domain=127.0.0.1; path=/

Cache-Control: private

X-Cache: MISS from web-s57.127.0.0.1

X-Cache-Lookup: MISS from web-s57.127.0.0.1:80

Connection: keep-alive

GET /person/resume_index.asp?t=62062.7 HTTP/1.1

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,

application/x-shockwave-flash, application/vnd.ms-excel,

application/vnd.ms-powerpoint, application/msword, */*

Referer: http://127.0.0.1/login/login.asp

Accept-Language: zh-cn

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)

Host: 127.0.0.1

Connection: Keep-Alive

Cache-Control: no-cache

Cookie: strloginusertype=1;

__zpWAM=1210926948718.119132.1211187954.1211188187.8;

firstchannelurl=http%3A//127.0.0.1/login/login.asp%3FBkUrl%3D%252Fmyzhaopin%252Fresume%255Fnav%252Easp%253FDYWE%253D1210926948718%252E119132%252E1210927448%252E1211158566%252E3%2526firstRef%253D%252D;

lastchannelurl=; JSShowname=kingseo%40163%2Ecom;

JSloginnamecookieindex=kingseo%40163%2Ecom; myzl111113171=0;

__zpWAMs1=1; __zpWAMs2=1; JSpUserInfo=;

JSsUserInfo=342C713654365A611D350769076B502C063658365C611D350D697A6B2F2C08360236026142355169456B042C5A3629365A611A350569186B022C5A3604365061793578690B6B5A2C7B3627365661173562697B6B5C2C0E362C36266111355D695F6B0F2C52361A360E6143357669076B572C06364736086143355B690D6B322C613654365A6117357769626B5C2C003644365A6115351669076B562C0F3658365F6117357569726B5C2C053652363E6178350B69076B5A2C60362836566166356469076B502C0436583658611D350169076B572C0E3628362761113506690D6B9

HTTP/1.0 200 OK

Date: Mon, 19 May 2008 09:14:22 GMT

Server: Microsoft-IIS/6.0

X-Powered-By: ASP.NET

Content-Type: text/html; Charset=utf-8

Set-Cookie:

JSsUserInfo=347320664E73566A466559614264577340675B77526850735F663F73296A4A6503611A6408731467197700680E7324664273516A44654661106409731E67517730682F7359664873296A3965556148642473376757775E6823732A664E735C6A236525614E645D733667277758680A730D661D73006A04650D611C64267342675C7756684F7307661C730A6A4C653B6127645B734267517724683573596646734A6A466551615364577344675077546855735F663773236A4A655361376428734E67517729682C7359664873246A3365556143645D7332672677586851735F662673266A4A6522612164577342675B775468527355664473566A41655361306427734E6751772168207359664873326A2365556142645D732;

domain=127.0.0.1; path=/

Set-Cookie: monitorlogin=Y; path=/

Set-Cookie: strloginusertype=4; expires=Tue, 31-Dec-2019 16:00:00 GMT;

domain=127.0.0.1; path=/

Cache-Control: private

X-Cache: MISS from web-s46.127.0.0.1

X-Cache-Lookup: MISS from web-s46.127.0.0.1:80

Via: 1.0 web-s46.127.0.0.1 (squid/3.0.STABLE4)

Connection: keep-alive

Content-Encoding: gzip

Content-Length: 3477

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值