我用httpclient 登录开心网主页的时候可以,但是我登录www.asd10000.com为什么就不行。
是不是我路径有问题。代码如下:
PostMethod post = new PostMethod("http://www.asd10000.com/app/member/login.php");
NameValuePair ie = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
// NameValuePair url = new NameValuePair("url", "/login/");
NameValuePair action = new NameValuePair("action", "/login");
NameValuePair username = new NameValuePair("username","ken313");
NameValuePair password = new NameValuePair("passwd", "aaa111");
NameValuePair langx = new NameValuePair("langx", "zh-cn");
post.setRequestBody(new NameValuePair[] {username, password,action,langx});
client.executeMethod(post);
System.out.println("******************************登录******************************");
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
System.out.println("******************************页面转向******************************");
String newUrl="http://www.asd10000.com/app/member/";
System.out.println("==========Cookies============");
int i=0;
for(Cookie c:cookies){
System.out.println(++i+": "+c);
}
client.getState().addCookies(cookies);
post.releaseConnection();
GetMethod get = new GetMethod(newUrl);
get.setRequestHeader("Cookie", cookies.toString());
client.executeMethod(get);
String responseString = get.getResponseBodyAsString();
//登录后首页的内容
System.out.println(responseString);
是不是我路径有问题。代码如下:
PostMethod post = new PostMethod("http://www.asd10000.com/app/member/login.php");
NameValuePair ie = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
// NameValuePair url = new NameValuePair("url", "/login/");
NameValuePair action = new NameValuePair("action", "/login");
NameValuePair username = new NameValuePair("username","ken313");
NameValuePair password = new NameValuePair("passwd", "aaa111");
NameValuePair langx = new NameValuePair("langx", "zh-cn");
post.setRequestBody(new NameValuePair[] {username, password,action,langx});
client.executeMethod(post);
System.out.println("******************************登录******************************");
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
System.out.println("******************************页面转向******************************");
String newUrl="http://www.asd10000.com/app/member/";
System.out.println("==========Cookies============");
int i=0;
for(Cookie c:cookies){
System.out.println(++i+": "+c);
}
client.getState().addCookies(cookies);
post.releaseConnection();
GetMethod get = new GetMethod(newUrl);
get.setRequestHeader("Cookie", cookies.toString());
client.executeMethod(get);
String responseString = get.getResponseBodyAsString();
//登录后首页的内容
System.out.println(responseString);
NameValuePair username = new NameValuePair("username","ken313");
NameValuePair password = new NameValuePair("passwd", "aaa111");
NameValuePair langx = new NameValuePair("langx", "zh-cn");
你确定 这几个参数是 http://www.asd10000.com/app/member/login.php 需要的吗?
<input type=HIDDEN name="uid" value="">
<input type=HIDDEN name="langx" value="en-us">
<input type=HIDDEN name="mac" value="">
<input type=HIDDEN name="ver" value="">
<input type="hidden" name="JE" value="">
我看了例子,但是好像没有看见可以设置成browser。
楼主并没有处理Cookie, 那是提交域(包括隐藏域)HttpClient能处理所以http的请求,完全可以当成一个Browser看。