各位高人:我想用httpClient模拟登陆后,获取登陆后网页的信息,但是却无论如何也得不到登陆后网页的信息,整了好久也不知道解决,请各位高手知道指教一下.不胜感激.我首先用ieHttpHeaders
各位高人:我想用httpClient模拟登陆后,获取登陆后网页的信息,但是却无论如何也得不到登陆后网页的信息,整了好久也不知道解决,请各位高手知道指教一下.不胜感激.我首先用ieHttpHeaders获取到netyi.net(得益网)的COOKIE信息如下:
POST /jsLoginStatus.aspx?returnUrl=%2fjsLoginStatus.aspx%3fguid%3db71cdc08-8622-42de-a4ba-fbd395c032ca 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://netyi.net/jsLoginStatus.aspx
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; Maxthon; .NET CLR 2.0.50727)
Host: netyi.net
Content-Length: 53
Connection: Keep-Alive
Cache-Control: no-cache
COOKIE: __utma=7615405.1271095282.1176109184.1186976162.1189938905.6; __utmz=7615405.1186977812.5.7.utmccn=(organic)|utmcsr=google|utmctr=spring+in+action+%E4%B8%8B%E8%BD%BD|utmcmd=organic; cnzz02=6; rtime=1; ltime=1190001640890; cnzz_eid=1250906-http%3A//zyjarge.spaces.live.com/blog/cns%21939889bfd1aba233%21497.entry; dnttemplateid=0; ASP.NET_SessiOnId=0hiqkbiudskwzy45sgrenbme
UserName=zlh218&Password=000000&Submit.x=0&Submit.y=0
HTTP/1.1 302 Found
Date: Mon, 17 Sep 2007 04:00:06 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Location: /jsLoginStatus.aspx?guid=e1579e4a-0a08-48c8-a52f-e8110820dd80
Set-COOKIE: Passport=zlh218; domain=.netyi.net; path=/
Set-COOKIE: validateCode=88E67E5A0FA31EA4D710A2E00A28C414; domain=.netyi.net; path=/
Set-COOKIE: dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; path=/
Set-COOKIE: dnttemplateid=0; expires=Tue, 11-Aug-2009 14:39:06 GMT; path=/
Set-COOKIE: dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; path=/
Set-COOKIE: lastolupdate=1043693968; path=/
Set-COOKIE: .ASPXAUTH=6C2E31EECFB08DFF79EA98EAAB54AB30D2CBA7A5CBEB5609312C30989F56CF07DDB84D281E03DD6AF89B4070918C20D25E0195D8F4AB9193E08AA9B0FCA53B16405AFCFD166DBEC7B30340F46E325669; domain=.netyi.net; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=gb2312
Content-Length: 178
我的代码如下:
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost( "netyi.net" , 80, "http" );
client.getParams().setCOOKIEPolicy(COOKIEPolicy.BROWSER_COMPATIBILITY);
PostMethod post = new PostMethod("/jsLoginStatus.aspx?returnUrl=%2fjsLoginStatus.aspx%3fguid%3db71cdc08-8622-42de-a4ba-fbd395c032ca" );
//NameValuePair chk = new NameValuePair( "chk" , "yes" );
//NameValuePair re = new NameValuePair( "re_log" , "yes" );
NameValuePair name = new NameValuePair( "UserName" , "zlh218" );
NameValuePair pass = new NameValuePair( "Password" , "000000" );
NameValuePair x = new NameValuePair( "Submit.x" , "0" );
NameValuePair y = new NameValuePair( "Submit.y" , "0" );
NameValuePair submit=new NameValuePair( "Submit" , "登 录" );
post.setRequestHeader("Referer", "http://netyi.net/jsLoginStatus.aspx");
post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
post.setRequestHeader("Content-Length", "53");
post.setRequestHeader("COOKIE", "Passport=zlh218; domain=.netyi.net; validateCode=88E67E5A0FA31EA4D710A2E00A28C414; domain=.netyi.net; dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; dnttemplateid=0; expires=Tue, 11-Aug-2009 14:39:06 GMT; " +
"dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; lastolupdate=1043693968; .ASPXAUTH=6C2E31EECFB08DFF79EA98EAAB54AB30D2CBA7A5CBEB5609312C30989F56CF07DDB84D281E03DD6AF89B4070918C20D25E0195D8F4AB9193E08AA9B0FCA53B16405AFCFD166DBEC7B30340F46E325669; domain=.netyi.net; path=/");
post.setRequestBody( new NameValuePair[]{name,pass,x,y,submit});
try{
int status = client.executeMethod(post);
System.out.println(status);
if (status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_MOVED_PERMANENTLY) {
Header locationHeader = post.getResponseHeader("location");
if (locationHeader != null) {
String redirectUri = locationHeader.getValue();
if (redirectUri == null || "".equals(redirectUri)) {
redirectUri = "/";
}
//client.getState().setCredentials("netyi.net","realm",new UsernamePasswordCredentials("zlh218", "000000"));
GetMethod get = new GetMethod(redirectUri);
get.setDoAuthentication(true);
client.executeMethod(get);
System.err.println("get redirect:");
Header[] headers = get.getResponseHeaders();
for (int i = 0; i < headers.length; ++i) {
System.err.println(headers[i].toString());
}
BufferedReader bf = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
String s = null;
while ((s=bf.readLine()) != null)
System.out.println(s);
get.releaseConnection();
}
}
}catch(Exception e){
e.printStackTrace();
}
运行后却始终得到是未登陆前的网页信息,不知道是怎么回事,请高手指点!100分
12 个解决方案
各位高人,快点给答案啊,好急!!!!!!!!!!!!!!!!!
wangc4(wang),你能给我答案吗?非常感激
多数是登陆错误吧,检测用户名密码的正确性,
NameValuePair submit=new NameValuePair( "Submit" , "登 录" );
看看这段后台获得的是什么,如果是乱码就要转码了
可能是你登录以后的页面发生跳转了吧。
你用这个语句看看:
Header header = post.getResponseHeader("Location");
if(header != null){
//页面有跳转
locateURL = header.getValue();
System.out.println("RedirectPage : "+locateURL);
}
然后在去连接跳转后的页面,就能拿到页面的数据了。
/**
这是修改后的代码,只改正了两处:
1:get前加上COOKIE
2:get时加上HOST
*/
final String HOST="http://www.netyi.net";
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("www.netyi.net", 80, "http");
client.getParams().setCOOKIEPolicy(COOKIEPolicy.BROWSER_COMPATIBILITY);
PostMethod post = new PostMethod(
"/jsLoginStatus.aspx?returnUrl=%2fjsLoginStatus.aspx%3fguid%3db71cdc08-8622-42de-a4ba-fbd395c032ca");
// NameValuePair chk = new NameValuePair( "chk" , "yes" );
// NameValuePair re = new NameValuePair( "re_log" , "yes" );
NameValuePair name = new NameValuePair("UserName", "zlh218");
NameValuePair pass = new NameValuePair("Password", "000000");
NameValuePair x = new NameValuePair("Submit.x", "0");
NameValuePair y = new NameValuePair("Submit.y", "0");
NameValuePair submit = new NameValuePair("Submit", "登 录");
post.setRequestHeader("Referer", "http://netyi.net/jsLoginStatus.aspx");
post.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
post.setRequestHeader("Content-Length", "53");
post
.setRequestHeader(
"COOKIE",
"Passport=zlh218; domain=.netyi.net; validateCode=88E67E5A0FA31EA4D710A2E00A28C414; domain=.netyi.net; dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; dnttemplateid=0; expires=Tue, 11-Aug-2009 14:39:06 GMT; "
+ "dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; lastolupdate=1043693968; .ASPXAUTH=6C2E31EECFB08DFF79EA98EAAB54AB30D2CBA7A5CBEB5609312C30989F56CF07DDB84D281E03DD6AF89B4070918C20D25E0195D8F4AB9193E08AA9B0FCA53B16405AFCFD166DBEC7B30340F46E325669; domain=.netyi.net; path=/");
post.setRequestBody(new NameValuePair[] { name, pass, x, y, submit });
try {
int status = client.executeMethod(post);
System.out.println(status);
//打印服务器返回的状态
System.out.println(post.getStatusLine());
//code=302 redirect page
if (status == HttpStatus.SC_MOVED_TEMPORARILY
|| status == HttpStatus.SC_MOVED_PERMANENTLY) {
Header locationHeader = post.getResponseHeader("location");
if (locationHeader != null) {
String redirectUri = locationHeader.getValue();
if (redirectUri == null || "".equals(redirectUri)) {
redirectUri = "/";
}
System.out.println("redirecturl:"+redirectUri);
// client.getState().setCredentials("netyi.net","realm",new
// UsernamePasswordCredentials("zlh218", "000000"));
GetMethod get = new GetMethod(HOST+redirectUri);
get.setDoAuthentication(true);
get
.setRequestHeader(
"COOKIE",
"Passport=zlh218; domain=.netyi.net; validateCode=88E67E5A0FA31EA4D710A2E00A28C414; domain=.netyi.net; dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; dnttemplateid=0; expires=Tue, 11-Aug-2009 14:39:06 GMT; "
+ "dnt=userid=802856&password=K8UyuJGWSS%2bbJ%2fduDu3plC07d%2fl6D%2b%2bqxIsCtqLlc0oTTWu77t9g3w%3d%3d&tpp=0&ppp=0&pmsound=0&invisible=0&referer=index.aspx&sigstatus=0&expires=-1; lastolupdate=1043693968; .ASPXAUTH=6C2E31EECFB08DFF79EA98EAAB54AB30D2CBA7A5CBEB5609312C30989F56CF07DDB84D281E03DD6AF89B4070918C20D25E0195D8F4AB9193E08AA9B0FCA53B16405AFCFD166DBEC7B30340F46E325669; domain=.netyi.net; path=/");
client.executeMethod(get);
System.err.println("get redirect:");
Header[] headers = get.getResponseHeaders();
for (int i = 0; i < headers.length; ++i) {
System.err.println(headers[i].toString());
}
BufferedReader bf = new BufferedReader(
new InputStreamReader(get.getResponseBodyAsStream()));
String s = null;
while ((s = bf.readLine()) != null)
System.out.println(s);
get.releaseConnection();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
request.getHeader("User-Agent")
获取一个这个表单的各个域是什么,然后用HTTPCLIENT放进去就可以了
我用过HTTPCLIENT,功能很强大,提交表单登录是很容易实现的
NameValuePair submit=new NameValuePair( "Submit " , "登 录 " ); 这句什么意思?这个不是应该是submit按钮的值吗?这个要写吗?请教了。