HttpClient浅尝-小试牛刀
来源:互联网 发布:第三方javascript编程 编辑:程序博客网 时间:2024/06/06 03:47
今天,突然想重新研究下HttpClient的应用,故写了个小程序。本程序纯属个人突发奇想,如有雷同,纯属巧合。
功能说明:通过httpclient方式登录人人网,并将个人首页、我的好友页面在本地生成为html文件,做了简单的任务,定时刷新生成页面。此程序只做为浅尝的小例子,不一定完全正确,参考了部分网站,仅仅个人实验。欢迎各位大侠拍砖、指点。
上代码:
1. HttpClient业务逻辑类 HttpClientService.java
package com.jushi.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class HttpClientService {
private HttpClient httpClient;
public HttpClientService(String host){
if(httpClient == null){
httpClient = new HttpClient();
}
httpClient.getHostConfiguration().setHost(host, 80, "http");
}
public HttpClientService(){
if(httpClient == null){
httpClient = new HttpClient();
}
httpClient.getHostConfiguration().setHost("", 80, "http");
}
/**
* 通过用户名密码post到指定服务端
* @param user
* @param userName
* @param password
* @param userPassword
* @param url
* @return
* @throws Exception
*/
public String login(String user, String userName, String password, String userPassword, String url) throws Exception{
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
PostMethod post = new PostMethod(url);
NameValuePair nuser = new NameValuePair(user, userName);
NameValuePair npass = new NameValuePair(password, userPassword);
return postToServer(new NameValuePair[]{nuser, npass}, post);
}
/**
* 通过 email 为用户名、password参数 post到指定服务端
* @param email
* @param password
* @param url
* @return
* @throws Exception
*/
public String login(String email, String password, String url) throws Exception{
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
PostMethod post = new PostMethod(url);
NameValuePair nemail = new NameValuePair("email", email);
NameValuePair npass = new NameValuePair("password", password);
return postToServer(new NameValuePair[]{nemail, npass}, post);
}
/**
* 通过post形式携带参数对发送到指定服务端
* @param nameValuePairs
* @param post
* @return
* @throws Exception
*/
public String postToServer(NameValuePair[] nameValuePairs, PostMethod post) throws Exception{
String redicretURL = null;
post.setRequestBody(nameValuePairs);
httpClient.executeMethod(post);
post.releaseConnection();
int statusCode = post.getStatusCode();
if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
|| (statusCode == HttpStatus.SC_MOVED_PERMANENTLY)
|| (statusCode == HttpStatus.SC_SEE_OTHER)
|| (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = post.getResponseHeader("location");
if (header != null) {
redicretURL = new String(header.getValue());
}
}
return redicretURL;
}
/**
* 返回InputStream流
* @param url
* @return
* @throws Exception
*/
public InputStream getConnectAsStream(String url) throws Exception {
GetMethod get = new GetMethod(url);
httpClient.executeMethod(get);
return get.getResponseBodyAsStream();
}
/**
* 返回String流
* @param url
* @return
* @throws Exception
*/
public String getConnectAsString(String url) throws Exception {
GetMethod get = new GetMethod(url);
httpClient.executeMethod(get);
return get.getResponseBodyAsString();
}
/**
* 返回byte[]流
* @param url
* @return
* @throws Exception
*/
public byte[] getConnectAsBytes(String url) throws Exception {
GetMethod get = new GetMethod(url);
httpClient.executeMethod(get);
return get.getResponseBody();
}
/**
* 通过byte[] 创建文件
* @param fileName
* @param response
* @return
* @throws Exception
*/
public boolean createFile(String fileName, byte[] response) throws Exception{
String path = getProperties("path.properties").getProperty("response.html.path");
File dir = new File(path);
if(!dir.exists()){
dir.mkdirs();
if(!dir.mkdirs()){
dir.mkdirs();
}
}
File file = new File(path + fileName);
FileOutputStream fos = null;
try{
fos = new FileOutputStream(file);
fos.write(response);
return true;
}catch(IOException e){
e.printStackTrace();
return false;
}finally{
try{
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
* 取得Properties
* @param path
* @return
*/
public static Properties getProperties(String path) {
try{
ClassLoader cl = HttpClientService.class.getClassLoader();
InputStream is = cl.getResourceAsStream(path);
Properties props = new Properties();
props.load(is);
return props;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
2. 登录客户端 LoginClient.java
package com.jushi.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;
import com.jushi.service.HttpClientService;
public class LoginClient {
public static void main(String as[]) throws Exception{
try {
fillForm();
if(login(Inits.urlProfiles, Inits.urlFriends, Inits.userName, Inits.passWord)){
Timer timer = new Timer();
timer.schedule(new httpClientTask(), 3000, 10000); //3s后开始,间隔10s
while(true){
try{
int ch = System.in.read();
if(ch - 'c' == 0){ //键盘输入 c 结束任务.
timer.cancel();
System.out.println("任务结束... ...");
System.exit(0);//exit program
}
}catch(Exception e){
e.printStackTrace();
}
}
}else
fillForm();
}catch(IOException E){
System.out.println("发生I/O错误!!! ");
}
}
/**
* 填写用户名密码
*/
public static void fillForm() {
InputStreamReader reader = null;
BufferedReader bufin = null;
try{
reader = new InputStreamReader(System.in);
bufin = new BufferedReader(reader);
Inits.urlProfiles = HttpClientService.getProperties("path.properties").getProperty("url.login");
Inits.urlFriends = HttpClientService.getProperties("path.properties").getProperty("url.friends");
System.out.println("<<<<< 登录renren网 >>>>> /r/n请输入用户名:");
Inits.userName = bufin.readLine();
System.out.println("请输入密码:");
Inits.passWord = bufin.readLine();
System.out.println("logining... ...");
}catch(Exception e){
e.printStackTrace();
}
}
public static class Inits{
public static String userName = "";
public static String passWord = "";
public static String urlProfiles = new String();
public static String urlFriends = new String();
public static StringBuffer index = new StringBuffer();
}
/**
* 处理登陆后业务
* @param urlProfiles
* @param urlFriends
* @param userName
* @param passWord
* @return
* @throws Exception
*/
public static boolean login(String urlProfiles, String urlFriends, String userName, String passWord) throws Exception{
HttpClientService hcs = new HttpClientService();
String url = hcs.login(userName, passWord, urlProfiles);
if(url != null){
Inits.index.append(hcs.getConnectAsString(urlProfiles));
System.out.println("Response: "+Inits.index.toString());
if(hcs.createFile("myRenRen.html", hcs.getConnectAsBytes(urlProfiles)))
System.out.println("生成个人主页文件:OK!");
else
System.out.println("生成个人主页文件:Failed!");
if(hcs.createFile("friends.html", hcs.getConnectAsBytes(urlFriends)))
System.out.println("生成好友列表文件:OK!");
else
System.out.println("生成好友列表文件:Failed!");
return true;
}else{
System.out.println("您输入的用户名或密码错误!请重新输入!");
return false;
}
}
}
class httpClientTask extends java.util.TimerTask{
public void run(){
try{
LoginClient.login(LoginClient.Inits.urlProfiles, LoginClient.Inits.urlFriends, LoginClient.Inits.userName, LoginClient.Inits.passWord);
}catch(Exception e){
e.printStackTrace();
}
}
}
3. 路径资源文件 path.properties
##访问的httpClient的URL
url.login=http://www.renren.com/PLogin.do
url.friends=http://friend.renren.com/myfriendlistx.do#item_1
##生成文件路径
response.html.path=F:/renren/
4. 所需jar包: commons-codec-1.3.jar、commons-httpclient-3.1.jar、commons-logging-1.0.4.jar 网上都有。
效果:输入正确的用户名密码登录成功后,通过返回的byte[]流生成了两个静态html文件。在本地可以查看。如果想生成其他文件,可以自定义。
5.工程结构图:
综上所述,本程序仅仅是HttpClient的小试牛刀。欢迎大家拍砖。
- HttpClient浅尝-小试牛刀
- spingmvc浅尝之一:小试牛刀
- 浅尝
- PhotoShop浅尝
- CorelDRAW浅尝
- 浅尝触发器
- 浅尝DotNetNuke
- 浅尝Linux
- 浅尝JQchart!
- DWR浅尝
- 浅尝Mina
- 浅尝python
- 浅尝Kivy
- 浅尝sed
- 浅尝awk
- 浅尝 SCALA
- Makefile浅尝
- 浅尝JMX
- C#通過相對路徑調用C++的dll
- 禁止复制手机号 分享网页
- qt no such file or directory 编译
- 并口定义及其编程基础之四:IIC调试工具的命令解析及使用
- 使用表达式树动态构建Linq查询条件来实现单个实体动态查询
- HttpClient浅尝-小试牛刀
- MySQL攻击部分代码
- 利用ArcGIS开发地理信息系统入门
- 用 awk命令 来保存记录CPU占有率
- IMF总裁之争白热化 法财长、墨央行行长忙拉票
- 内存整理的原理是什么?
- AndroidGUI21:ImageSwitcher常用技巧
- 002. EsayUI系列之easyui-menu
- 聚焦“钱流” 热钱流向古典家具