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不久.....
谢谢你的浏览~~~~
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不久.....
谢谢你的浏览~~~~
解决方案 »
- jsp页面pdf
- javascript 图片上传预览 无法预览
- 生成的xml为什么没有缩进
- J2EE即出,谁与争风;.NET王者归来,雄霸天下!
- 征集优秀译者:翻译O'reilly好书《Enterprise JavaBeans 3.0,5E》!
- loverisyour(我是民工又咋的,不能搞开发啊!)请进来领分!
- java是怎样处理事务的?
- struts1.1下<html:options>tag怎么用?能不能给个完整的例子?
- 写struts时,程序报错,看不懂!
- 线程问题(初学者)
- 关于spring的SqlMapClientTemplate问题。
- Path cn.itcast.LoginAction does not start with a "/" character
httpclient的jar包下载地址是这个~~
http://labs.renren.com/apache-mirror/httpcomponents/commons-httpclient/binary/commons-httpclient-3.1.zip