作者:平安 | 来源:互联网 | 2023-05-19 05:50
模拟用户登陆-登陆成功获取随机的cookie利用此cookie对http:61.144.195.168PhoneReaddefault.aspx进行发送文章里的代码里注释里的那一块数据以及登陆
模拟用户登陆-登陆成功获取随机的COOKIE利用此COOKIE对http://61.144.195.168/PhoneRead/default.aspx进行发送文章里的代码里注释里的那一块数据以及登陆得到的COOKIE!
最下方的是我的源码, 已经取到了登陆时所需要的COOKIE了。 做到我卡主了不知道怎么对http://61.144.195.168/PhoneRead/default.aspx发送COOKIE!对HttpClient不太熟帮帮忙= =。
package com.newpalm.unicomfetch.threads;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import org.apache.commons.httpclient.COOKIE;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.COOKIE.COOKIEPolicy;
import org.apache.commons.httpclient.COOKIE.COOKIESpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.visitors.ObjectFindingVisitor;
/**
* 用来演示登录表单的示例
*
*/
public class FormLoginDemo {
public static void main(String[] args) throws Exception{
String s ="登 录";
String s1 ="abc123";
Parser parser = null;
ObjectFindingVisitor visitor = null;
HttpClient client = new HttpClient();
client.getParams().setContentCharset("GBK");
// method.addParameter("login", "登录");
//模拟登录页面
PostMethod post = new PostMethod("d");
NameValuePair __VIEWSTATE = new NameValuePair("__VIEWSTATE","/wEPDwUJODg1NzI5NTYwZGSqtd9cESQGNA00lLD3T6Z/0TiG4w==");
NameValuePair __EVENTVALIDATION = new NameValuePair("__EVENTVALIDATION","/wEWBAKY3ZTzAwKl1bKzCQK1qbSRCwLCi9reA99MBLTab8Vojx1AKgKrre0MPlRd");
NameValuePair __EVENTTARGET = new NameValuePair("__EVENTTARGET","");
NameValuePair __EVENTARGUMENT = new NameValuePair("__EVENTARGUMENT","");
NameValuePair txtUserName = new NameValuePair("txtUserName",s1);
NameValuePair txtPassword = new NameValuePair("txtPassword","123789");
NameValuePair btnSubmit = new NameValuePair("btnSubmit",s);
post.setRequestBody(new NameValuePair[]{__EVENTTARGET,__EVENTARGUMENT,__VIEWSTATE,__EVENTVALIDATION,txtUserName,txtPassword,btnSubmit});
int status = client.executeMethod(post);
post.releaseConnection();
//查看COOKIE信息
COOKIESpec COOKIEspec = COOKIEPolicy.getDefaultSpec();
COOKIE[] COOKIEs = client.getState().getCOOKIEs();
if (COOKIEs.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < COOKIEs.length; i++) {
System.out.println(COOKIEs[i].toString()); //得到新的COOKIE
}
}
/*
PostMethod pt = new PostMethod("http://61.144.195.168/PhoneRead/default.aspx"); //对新的URL进行post以下数据以及一个登陆时获取的COOKIE!
NameValuePair __VIEWSTATE2= new NameValuePair("__VIEWSTATE", "/wEPDwUJMzc4NDIwMDA5D2QWAgIDD2QWBAIFDxBkDxYBZhYBEAUM6ZqP6ZiF5Lmm5Z+OBQzpmo/pmIXkuabln45nZGQCBw8QDxYGHg5EYXRhVmFsdWVGaWVsZAUCSUQeDURhdGFUZXh0RmllbGQFCkNoYW5uZWxOdW0eC18hRGF0YUJvdW5kZ2QPFgICAgIDFgIQBQwwMjAwMjAwMzQwMDcFAzc1MmcQBQswNTAwMTAwNTU3OQUDNzk0Z2RkGAEFCGd2RGV0YWlsD2dkNluMBLPjjOtL0bcj6MAECxEc2ms=");
NameValuePair __EVENTVALIDATION2 = new NameValuePair("__EVENTVALIDATION", "/wEWCwKMp47hDgLg2ZN+AsKGtEYC+OjGlwICrMHw9QgCs+GQ8Q4Ct67r/gsC4f3+FQLtgM2ICAKBlYDZCwLCi9reA/uS0jd+R+EbTMk75431NHsvKK5Y");
NameValuePair txtStartDate = new NameValuePair("txtStartDate", "2011-01-11");
NameValuePair txtEndDate = new NameValuePair("txtEndDate", "2011-02-11");
NameValuePair ddlProductType = new NameValuePair("ddlProductType.x", "随阅书城");
NameValuePair ddlChannelNum = new NameValuePair("ddlChannelNum", "no");
NameValuePair ddlType = new NameValuePair("ddlType", "day");
NameValuePair btnSubmit2 = new NameValuePair("btnSubmit", "查 询");
pt.setRequestBody(new NameValuePair[]{__VIEWSTATE2,__EVENTVALIDATION2,txtStartDate,txtEndDate,ddlProductType,ddlChannelNum,ddlType,btnSubmit2});
*/
}
12 个解决方案
想办法获得response对象吧,不然COOKIE没法写啊!~
Header headerResponse = postMethod.getResponseHeader(COOKIE); 是这个不?
搞定了,登录获取COOKIE。利用COOKIE访问第二个页面! 代码写的不好分享下!
package com.newpalm.unicomfetch.threads;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.COOKIE;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
*
*@xiaozhong
*/
public class FormLoginDemo {
public static void main(String[] args) throws Exception{
String s ="登 录";
String s1 ="admin"; //账号
HttpClient client = new HttpClient();
client.getParams().setContentCharset("GBK");
//模拟登录页面
PostMethod post = new PostMethod("http://localhost/PhoneRead/login.aspx");
NameValuePair __VIEWSTATE = new NameValuePair("__VIEWSTATE","/wEPDwUJODg1NzI5NTYwZGSqtd9cESQGNA00lLD3T6Z/0TiG4w==");
NameValuePair __EVENTVALIDATION = new NameValuePair("__EVENTVALIDATION","/wEWBAKY3ZTzAwKl1bKzCQK1qbSRCwLCi9reA99MBLTab8Vojx1AKgKrre0MPlRd");
NameValuePair __EVENTTARGET = new NameValuePair("__EVENTTARGET","");
NameValuePair __EVENTARGUMENT = new NameValuePair("__EVENTARGUMENT","");
NameValuePair txtUserName = new NameValuePair("txtUserName",s1);
NameValuePair txtPassword = new NameValuePair("txtPassword","123789");
NameValuePair btnSubmit = new NameValuePair("btnSubmit",s);
post.setRequestBody(new NameValuePair[]{__EVENTTARGET,__EVENTARGUMENT,__VIEWSTATE,__EVENTVALIDATION,txtUserName,txtPassword,btnSubmit});
int status = client.executeMethod(post);
post.releaseConnection();
//查看COOKIE信息
// COOKIESpec COOKIEspec = COOKIEPolicy.getDefaultSpec();
COOKIE[] COOKIEs = client.getState().getCOOKIEs();
if (COOKIEs.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < COOKIEs.length; i++) {
System.out.println("i:"+COOKIEs[i].toString()); //获取的COOKIE
}
}
NameValuePair __VIEWSTATE2= new NameValuePair("__VIEWSTATE", "/wEPDwUJMzc4NDIwMDA5D2QWAgIDD2QWBAIFDxBkDxYBZhYBEAUM6ZqP6ZiF5Lmm5Z+OBQzpmo/pmIXkuabln45nZGQCBw8QDxYGHg5EYXRhVmFsdWVGaWVsZAUCSUQeDURhdGFUZXh0RmllbGQFCkNoYW5uZWxOdW0eC18hRGF0YUJvdW5kZ2QPFgICAgIDFgIQBQwwMjAwMjAwMzQwMDcFAzc1MmcQBQswNTAwMTAwNTU3OQUDNzk0Z2RkGAEFCGd2RGV0YWlsD2dkNluMBLPjjOtL0bcj6MAECxEc2ms=");
NameValuePair __EVENTVALIDATION2 = new NameValuePair("__EVENTVALIDATION", "/wEWCwKMp47hDgLg2ZN+AsKGtEYC+OjGlwICrMHw9QgCs+GQ8Q4Ct67r/gsC4f3+FQLtgM2ICAKBlYDZCwLCi9reA/uS0jd+R+EbTMk75431NHsvKK5Y");
NameValuePair txtStartDate = new NameValuePair("txtStartDate", "2011-01-20");
NameValuePair txtEndDate = new NameValuePair("txtEndDate", "2011-01-23");
NameValuePair ddlProductType = new NameValuePair("ddlProductType.x", "aa");
NameValuePair ddlChannelNum = new NameValuePair("ddlChannelNum", "no");
NameValuePair ddlType = new NameValuePair("ddlType", "day");
NameValuePair btnSubmit2 = new NameValuePair("btnSubmit", "查 询");
// (1)构造HttpClient的实例
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("GBK");
// (2)创建POST方法的实例
PostMethod postMethod = new PostMethod("http://localhost/PhoneRead/default.aspx");
// (3)设置http request头
List headers = new ArrayList();
headers.add(new Header("COOKIE", COOKIEs[0].toString()));
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers); //发送头信息
postMethod.setRequestBody(new NameValuePair[]{__VIEWSTATE2,__EVENTVALIDATION2,txtStartDate,txtEndDate,ddlProductType,ddlChannelNum,ddlType,btnSubmit2});
// 使用系统提供的默认的恢复策略
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try
{
// (4)执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK)
{
System.err.println("Method failed: " + postMethod.getStatusLine());
}
// (5)读取response头信息
// Header headerResponse = postMethod .getResponseHeader("COOKIE");
// String headerStr = headerResponse.getValue();
// String headerStr = httpClient.getState().get
// (6)读取内容
byte[] responseBody = postMethod.getResponseBody();
// (7) 处理内容
// System.out.println(headerStr);
System.out.println(new String(responseBody));
} catch (HttpException e)
{
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
// 释放连接
postMethod.releaseConnection();
}
}
}