`
weiwei5910
  • 浏览: 26598 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类

初学java,未有小成 --- httpClient4.jar包学习

阅读更多
package com.wei.study;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 初学java,未有小成 --- httpClient4.jar包学习  
 * 看到别人针对12306网站用httpClient4做了外挂
 * 很多工程中也引入了这个包
 * 于是上网找了些资料,小小的学习了一下
 * 使用httpClient4做个模拟登录
 * @author ww
 * @date 2012-1-12
 * @version 1.0
 */
public class ImitateLogin {
	
	public static void main(String[] args) throws ClientProtocolException, IOException, URISyntaxException {
		
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("username", "wwei11155"); //百度登陆用户名
		paramsMap.put("password", "252067756"); //百度登陆密码
		
		String cookie = sendPost("https://passport.baidu.com/?login",paramsMap,null);
		Map<String,String> headers = new HashMap<String,String>();
		headers.put("Cookie",cookie);
		
		String html = EntityUtils.toString(sendGet("http://passport.baidu.com/center",headers));
		String regex = "<img id=\"pa:bPortrait\" src=\".+\".+/>";
		html = getInfo(html,regex);
		System.out.println(html);
		JOptionPane.showMessageDialog(null, "<html><center>这是你设置的百度图像</center>"+html.replace(">", "")+"</html>");
		html = html.substring(html.indexOf("http"), html.lastIndexOf("jpg")+3);
		System.out.println(html);
		saveImage("e:/1.jpg",html);
	}
	
	
	/**
	 * 使用post方式请求网站内容   示例 url "http://www.baidu.com/s?wd=玩味"
	 * @param url 为 "www.baidu.com"
	 * @param query 可写为   "/s"
	 * @param paramsMap 参数  即 wd=玩味  存入Map中 key=wd , value=玩味 
	 * @param headers 请求头部信息 可将Cookie 放入其中
	 * @return String
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	public static String sendPost(String url,String query,Map<String, String> paramsMap,Map<String,String> headers) throws ClientProtocolException, IOException, URISyntaxException{
		DefaultHttpClient httpClient = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		if(paramsMap!=null){
			for(String str : paramsMap.keySet()){
				params.add(new BasicNameValuePair(str, paramsMap.get(str)));
			}
		}
		URI uri = URIUtils.createURI("http", url, -1, query, URLEncodedUtils.format(params, "UTF-8"), null);
		HttpPost httpPost = new HttpPost(uri);
		
		if (headers != null) {
			httpPost.setHeaders(assembHead(headers));
		}
		httpClient.getParams().setIntParameter("http.socket.timeout", 30000);
		HttpResponse response = httpClient.execute(httpPost);
		
		HttpEntity entity = response.getEntity();
		System.out.println(httpPost.getURI());
		System.out.println(EntityUtils.toString(entity));
		return assembCookies(httpClient.getCookieStore().getCookies());
	}
	
	
	/**
	 * 登陆 返回Cookie信息 
	 * Cookie 个人理解 
	 * 成功登陆某网站后 该网站返回给你一段加密的信息 它记录你的ID,密码 等等 相当一把钥匙
	 * 只该Cookie存在 再次访问该网站时 服务器如果取到了Cookie 就无需再登录了
	 * 由于我们是模拟登录并非真正的使用浏览器去登录  所以Cookie信息就没有保存在我们的计算机上 
	 * 但我们只要把该信息放在下一次请求的请求头中 那么也就不用再次登录了
 	 * @param url 需要请求的地址
	 * @param paramsMap 参数 
	 * @param headers 头部信息
	 * @return String 
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendPost(String url,Map<String, String> paramsMap,Map<String,String> headers) throws ClientProtocolException, IOException{
		DefaultHttpClient httpClient = new DefaultHttpClient();
		
		HttpPost httpPost = new HttpPost(url);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		if(paramsMap!=null){
			for(String str : paramsMap.keySet()){
				params.add(new BasicNameValuePair(str, paramsMap.get(str)));
			}
		}
		httpPost.setEntity(new UrlEncodedFormEntity(params));
		if (headers != null) {
			httpPost.setHeaders(assembHead(headers));
		}
		//设置超时时间 
		httpClient.getParams().setIntParameter("http.socket.timeout", 30000);
		HttpResponse response = httpClient.execute(httpPost);
		
		HttpEntity entity = response.getEntity();
		System.out.println(httpPost.getURI());
		System.out.println(EntityUtils.toString(entity));
		httpPost.abort();
		return assembCookies(httpClient.getCookieStore().getCookies());
	}
	
	
	/**
	 * get方法 
	 * HttpGet与HttpPost方法大致相同 
	 * 操作中发现HttpGet没有setEntity这个方法  
	 * 个人理解   
	 * Http定义了与服务器交互的不同方法,最基本的方法有4种
	 * get post put delete 全称为资源描述符 它们相对应的操作可理解为
	 *  查       改      增      删
	 * 因为get方法 主要是像服务器上获取数据   而post是向服务器传送数据
	 * 而setEntity方法相当于是把Entity传送给服务器 
	 * 所以post中有setEntity方法  get主要获取数据  所以就不需要了....
	 * @param url 请求地址
	 * @param headers 头部信息
	 * @return HttpEntity
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpEntity sendGet(String url,Map<String,String> headers) throws ClientProtocolException, IOException{
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		if (headers != null) {
			httpGet.setHeaders(assembHead(headers));
		}
		// 设置超时时间
		httpClient.getParams().setIntParameter("http.socket.timeout", 30000);
		HttpResponse response = httpClient.execute(httpGet);

		HttpEntity entity = response.getEntity();
		return entity;
	}
	
	
	/**
	 * 组装Cookies信息 返回出格式为 xxxx=xxxxxxx
	 * @param cookies
	 * @return String
	 */
	public static String assembCookies(List<Cookie> cookies){
		StringBuffer sb = new StringBuffer();
		for (Cookie cookie : cookies) {
			sb.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
		}
		System.out.println(sb.toString());
		return sb.toString();
	}
	
	/**
	 * 组装Header信息
	 * @param headers
	 * @return Header[]
	 */
	public static Header[] assembHead(Map<String, String> headers){
		Header[] allHeader = new BasicHeader[headers.size()]; 
		int i = 0;
		for(String str:headers.keySet()){
			allHeader[i] = new BasicHeader(str,headers.get(str));
			i++;
		}
		return allHeader;
	}
	
	
	/**
	 * 正则查找  
	 * 照葫芦画瓢
	 * @param content 要查找的字符串
	 * @param regex 正则表达式
	 * @return String
	 */
	public static String getInfo(String content,String regex){
		String rs = null;
		Pattern pattern = Pattern.compile(regex);
		Matcher mat = pattern.matcher(content);
		if(mat.find()){
			rs = mat.group();
		}
		return rs;
	}
	
	
	/**
	 * 保存图片到本地
	 * @param pathname 本地地址
	 * @param url 网络地址
	 * @throws IOException
	 */
	public static void saveImage(String pathname,String url) throws IOException{
		
		FileOutputStream fos = new FileOutputStream(new File(pathname));
		InputStream is = sendGet(url, null).getContent();
		int temp = 0;
		while((temp=is.read())!=-1){
			fos.write(temp);
		}
		is.close();
		fos.close();
	}
	
}


/**
 * 初学JAVA,未有小成;开源学习,多多交流!
 * weiwei5910@vip.qq.com
 */


分享到:
评论
2 楼 jianfaith 2013-03-12  
返回的html代码  有乱码现象 
1 楼 zzzzterrybogard 2012-04-05  
楼主V5

相关推荐

Global site tag (gtag.js) - Google Analytics