日期:2014-05-20  浏览次数:20713 次

Httpclient模拟登录网站时说我没有启动Cookies..
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.httpclient.params.*;

public class test
{

static final String LOGON_SITE = "http://admin.51network.com/wits/www/admin/index.php";
static final int LOGON_PORT = 80;

public static void main(String[] args) throws Exception
{
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT,"http");

//登录 
PostMethod post = new PostMethod("http://admin.51network.com/wits/www/admin/index.php");
DefaultHttpParams.getDefaultParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);

NameValuePair username = new NameValuePair("username", "123@test.com");
NameValuePair password = new NameValuePair("password", "asdfgag");
post.setRequestBody(new NameValuePair[] { username, password });
client.executeMethod(post);
String responseString = new String(post.getResponseBodyAsString().getBytes("gbk"));
System.out.println(responseString);
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
System.out.println("--------");
for(int i=0;i <cookies.length;i++) 

  System.out.print(cookies[i].toString()); 
}  
post.releaseConnection(); 
}
}



但是返回的页面总是说"You need to enable cookies before you can use Wits",就好像我没有启动IE的Cookies时候登录的情况一样.这个是为什么呢,只是第一次登录啊.

希望你们的指导,刚刚学习Java不久.....
谢谢你的浏览~~~~

------解决方案--------------------
不熟悉app,绑定
------解决方案--------------------
没启动COOKIE ?

禁用了么?
------解决方案--------------------
那我真的不懂了
友情支持
祝你好运