`
jushi1988
  • 浏览: 73614 次
  • 性别: Icon_minigender_1
  • 来自: 黑龙江
社区版块
存档分类
最新评论

HttpClient浅尝-小试牛刀

阅读更多


      今天,突然想重新研究下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,返回跳转URL	 
	 * @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文件。在本地可以查看。如果想生成其他文件,可以自定义。

  

  综上所述,本程序仅仅是HttpClient的小试牛刀。欢迎大家拍砖。

 

0
0
分享到:
评论

相关推荐

    httpclient5-fluent-5.0.3.jar

    这是我一个抠图程序发送请求时专用的jar,maven仓库不好下载,我就上传到csdn,以便下次可以继续使用

    httpclient-4.5.13-API文档-中文版.zip

    赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...

    httpclient-4.5jar

    httpclient-4.5所需jar包,里面包含httpclient-4.5.jar等等10个必须的开发包。 1.commons-codec-1.9.jar 2.commons-logging-1.2.jar 3.fluent-hc-4.5.jar 4.httpclient-4.5.jar 5.httpclient-cache-4.5.jar 6....

    httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5的jar包下载

    这里提到的四个jar包——httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5,都是Apache HttpClient库的不同组件,用于支持HTTP通信和相关功能。 **httpcore-4.2.4.jar** 是HTTP Core模块...

    httpclient-4.5.6-API文档-中文版.zip

    赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...

    httpclient-4.5.13-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...

    httpclient5-5.0.3.jar

    这是我一个抠图程序发送请求时专用的jar,maven仓库不好下载,我就上传到csdn,以便下次可以继续使用

    httpclient-4.5.12-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.12.jar; 赠送原API文档:httpclient-4.5.12-javadoc.jar; 赠送源代码:httpclient-4.5.12-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.12.pom; 包含翻译后的API文档:...

    httpclient-4.5.13-sources.jar

    httpclient-4.5.13-sources.jar

    httpclient-4.4.1-API文档-中文版.zip

    赠送jar包:httpclient-4.4.1.jar; 赠送原API文档:httpclient-4.4.1-javadoc.jar; 赠送源代码:httpclient-4.4.1-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.1.pom; 包含翻译后的API文档:httpclient...

    httpclient-4.5.10-API文档-中文版.zip

    赠送jar包:httpclient-4.5.10.jar; 赠送原API文档:httpclient-4.5.10-javadoc.jar; 赠送源代码:httpclient-4.5.10-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.10.pom; 包含翻译后的API文档:...

    httpclient-4.1-alpha1.jar

    httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar

    httpclient-4.5.6-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...

    httpclient-4.5.5-API文档-中文版.zip

    赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...

    httpclient-cache-4.5.jar

    用于http请求的jar包

    httpclient-4.5.2-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)-英语-对照...

    httpclient-4.4-API文档-中文版.zip

    赠送jar包:httpclient-4.4.jar; 赠送原API文档:httpclient-4.4-javadoc.jar; 赠送源代码:httpclient-4.4-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.pom; 包含翻译后的API文档:httpclient-4.4-...

    httpclient-4.5.6的jar包

    httpclient-4.5.6.jar, commons-codec-1.10.jar, commons-logging-1.2.jar , fluent-hc-4.5.6.jar, httpclient-cache-4.5.6.jar, httpclient-win-4.5.6.jar , httpcore-4.4.10.jar, httpmime-4.5.6.jar, jna-4.4.0....

    commons-httpclient-3.0.jar JAVA中使用HttpClient可以用到

    《JAVA中使用HttpClient:commons-httpclient-3.0.jar详解》 在JAVA开发中,进行HTTP请求时,Apache的HttpClient库是一个不可或缺的工具。本文将深入解析`commons-httpclient-3.0.jar`,它是HttpClient的一个重要...

    httpclient-4.5.5.jar

    httpclient-4.4.5.jar最新JAR下载dowonloaddowonloaddowonloaddowonloaddowonload

Global site tag (gtag.js) - Google Analytics