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", "[email protected]");
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不久.....
谢谢你的浏览~~~~

解决方案 »

  1.   

    但是我不是用浏览器啊,我是写个APP来模拟登录啊~~~~~~~~
      

  2.   

    没有禁用啊,我在Eclipse跑的时候,返回页面是说"You need to enable cookies before you can use Wits".然后我试着用浏览器登录,并禁了浏览器的Cookies的时候,返回的页面和在Eclipse跑的时候返回的一样.谢谢你们的浏览=,=迷茫
      

  3.   

    没人可以指导一下吗?~~~~~~
    httpclient的jar包下载地址是这个~~
    http://labs.renren.com/apache-mirror/httpcomponents/commons-httpclient/binary/commons-httpclient-3.1.zip
      

  4.   

    哦=,=表单漏了个值......原来每次访问时,就自动生成一个cookie..在页面提取出来一起传过去就好咯~~
      

  5.   

    客户端打开几个页面的,是不是需要cookies 还是session 追加到新打开的页面??