需要用到的java包
commons-httpclient-3.1.jar
commons-logging.jar
log4j-1.2.15.jar
commons-codec.jar
很简单,没什么好说的,直接上代码:
package httpclienttest;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class LoginKaixin {
private static final String LOGON_SITE = "http://www.kaixin001.com";
private 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);
//登录页面
PostMethod post = new PostMethod("http://www.kaixin001.com/login/login.php");
NameValuePair ie = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
NameValuePair url = new NameValuePair("url", "/home/");
NameValuePair username = new NameValuePair("email","xxx@163.com");
NameValuePair password = new NameValuePair("password", "xxxxxx");
post.setRequestBody(new NameValuePair[] { ie,url,username, password});
client.executeMethod(post);
System.out.println("******************************登录******************************");
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
System.out.println("******************************页面转向******************************");
String newUrl="http://www.kaixin001.com/home/";
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);
get.releaseConnection();
System.out.println("******************************组件功能******************************");
//"http://www.kaixin001.com/!slave/index.php", "朋友买卖"
//"http://www.kaixin001.com/!parking/index.php", "争车位"
//"http://www.kaixin001.com/!house/index.php?_lgmode=pri", "买房子"
//http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49
//"http://www.kaixin001.com/!house/garden/index.php","花园"
//(1)进入朋友买卖****************
System.out.println("******************************(1)进入朋友买卖******************************");
String slave="http://www.kaixin001.com/!slave/index.php";
get = new GetMethod(slave);
get.setRequestHeader("Cookie", cookies.toString());
client.executeMethod(get);
responseString = get.getResponseBodyAsString();
System.out.println(responseString);
get.releaseConnection();
//(2)进入争车位****************
System.out.println("******************************(2)进入争车位******************************");
String parking="http://www.kaixin001.com/!parking/index.php";
get = new GetMethod(parking);
get.setRequestHeader("Cookie", cookies.toString());
client.executeMethod(get);
responseString = get.getResponseBodyAsString();
System.out.println(responseString);
get.releaseConnection();
//(3)进入买房子****************
System.out.println("******************************(3)进入买房子*******************************");
String house="http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49";
get = new GetMethod(house);
get.setRequestHeader("Cookie", cookies.toString());
client.executeMethod(get);
responseString = get.getResponseBodyAsString();
System.out.println(responseString);
get.releaseConnection();
//(4)进入花园****************
System.out.println("******************************(4)进入花园*******************************");
String garden="http://www.kaixin001.com/!house/garden/index.php";
get = new GetMethod(garden);
get.setRequestHeader("Cookie", cookies.toString());
client.executeMethod(get);
responseString = get.getResponseBodyAsString();
System.out.println(responseString);
get.releaseConnection();
}
}
来源:http://www.iteye.com/topic/377346
分享到:
相关推荐
httpclient,jar,实例,例子,post,get
httpclient.jar包及其POST方法使用例子
NULL 博文链接:https://malixxx.iteye.com/blog/452695
学习C#必经之路,入门级知识总结,笔记本知识概况,走一步再走一步
httpClient例子,支持代理(去掉注释即可用代理),已封装好util(post,get),JAR也有,打开导入工程就可以用。
HttpClient模拟登录实例,模拟登录网站,获取用户信息,登录后数据,带验证码登录,带解析HTML实例。
HTTPClient组件的应用
httpclient testgn 接口测试
模拟登录 不提供用户名密码
其中有4.1.1jar包、httpclient说明文档doc格式、简单使用例子post\get方式都有,导入类可用。项目中使用很方便!
用HttpClient实现了 网易相册的登录
Java Httpclient的一个小例子,只有客户端的代码,服务器端随便写一个servlet都行,我也是新学的,主要是自己下载的jar包然后自己整合的
关于HttpClient的小例子,很简单,附件为项目源码,直接导入执行。注意包含了配置信息。
简单来说HttpClient就是模拟post或get的. 简化HTTP客户端与服务器进行各种通讯的开源东东 最新是4.0-alpha2,内含jar包及例子.
附件包含一个根据身份证号码调用必应接口返回个人身份证信息的例子,有JAR包和代码例子,可以直接拿来使用。
这是一个经典的java爬虫例子:使用httpclient4.4登录豆瓣,发表说说 代码中即有cookie的自动管理,又有验证码的处理。 许多人只会简单的爬,但是有的网站需要登录就不会弄了,再或者有的网站有验证码也不知道如何...
HttpClient之Https应用实例~ 包含: HttpClient 使用代理访问Https HttpClient 信任所有主机-对于任何证书都不做检查 HttpClient 允许所有证书的校验(包括过期证书)
比较全的httpclient入门介绍和例子代码
HttpClient代理访问网络
httpclient-4.0-alpha2.jar commons-codec-1.3.jar commons-httpclient-3.1.jar httpclient开发所有的包以及实例