HttpClient3高级应用

 

HttpClient3.1高级应用

 

1. 下载地址:http://hc.apache.org/downloads.cgi

 

2. GET方法

 

public class MyHttpClient extends HttpClient {
	static HttpClient myHttp = null;
	static int number = 0;

	public static HttpClient getHttpClient() {
		if (myHttp == null || number > 500) {
			myHttp = new HttpClient();
			myHttp.getHttpConnectionManager().getParams()
					.setConnectionTimeout(60000);
			number = 0;
		}
		number++;
		return myHttp;
	}
}

 

public static String doGet(String url, String setCode, String readCode) {
	String sTotalString = "";
	setCode = (setCode == null || "".equals(setCode)) ? "gb2312" : setCode;
	readCode = (readCode == null || "".equals(readCode)) ? "gb2312"
			: readCode;
	GetMethod get = new GetMethod(url);
	get.getParams().setHttpElementCharset(setCode);
	get.getParams().setCredentialCharset(setCode);
	get.getParams().setContentCharset(setCode);
	try {
		HttpClient httpClient = MyHttpClient.getHttpClient();
		int statusCode = httpClient.executeMethod(get);
		if (statusCode != HttpStatus.SC_OK) {
			return "查询失败!";
		}
		byte[] responseBody = get.getResponseBody();
		ByteArrayInputStream is = new ByteArrayInputStream(responseBody);
		BufferedReader l_reader = new BufferedReader(new InputStreamReader(
				is, readCode));
		String sCurrentLine = "";
		while ((sCurrentLine = l_reader.readLine()) != null) {
			sTotalString += sCurrentLine;
		}
	} catch (HttpException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		get.releaseConnection();
		sTotalString = (sTotalString == null) ? "查询失败" : sTotalString;
	}
	return sTotalString;
}

 

3. POST方法

 

public static String doPost(String url, NameValuePair[] param, String setCode, String readCode) {
	String sTotalString = "";
	setCode = (setCode == null || "".equals(setCode)) ? "gb2312" : setCode;
	readCode = (readCode == null || "".equals(readCode)) ? "gb2312"
			: readCode;
	PostMethod post = new PostMethod(url);
	post.getParams().setHttpElementCharset(setCode);
	post.getParams().setCredentialCharset(setCode);
	post.getParams().setContentCharset(setCode);
	post.setRequestBody(param);
	try {
		HttpClient httpClient = MyHttpClient.getHttpClient();
		int statusCode = httpClient.executeMethod(post);
		if (statusCode != HttpStatus.SC_OK) {
			return "查询失败!";
		}
		byte[] responseBody = post.getResponseBody();
		ByteArrayInputStream is = new ByteArrayInputStream(responseBody);
		BufferedReader l_reader = new BufferedReader(new InputStreamReader(
				is, readCode));
		String sCurrentLine = "";
		while ((sCurrentLine = l_reader.readLine()) != null) {
			sTotalString += sCurrentLine;
		}
	} catch (HttpException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		post.releaseConnection();
		sTotalString = (sTotalString == null) ? "查询失败" : sTotalString;
	}
	return sTotalString;
} 

 

4. 下载验证码图片 

 

public static void getYzm(String url, String Path) {
	PostMethod post = new PostMethod(url);
	try {
		HttpClient httpClient = MyHttpClient.getHttpClient();
		int statusCode = httpClient.executeMethod(post);
	} catch (HttpException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	try {
		InputStream i = post.getResponseBodyAsStream();
		if (i != null) {
			writePic(i, Path);
		}
	} catch (IOException e) {
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public static void download(String url, String path) {
	HttpClient client = MyHttpClient.getHttpClient();
	GetMethod get = new GetMethod(url);
	try {
		int statusCode = client.executeMethod(get);
		try {
			writePic(get.getResponseBody(), path);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} catch (HttpException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

public static void writePic(byte[] responseBody, String Path)
		throws Exception {
	ByteArrayInputStream is = new ByteArrayInputStream(responseBody);
	BufferedInputStream in = new BufferedInputStream(is);
	FileOutputStream fo = new FileOutputStream(Path);
	BufferedOutputStream out = new BufferedOutputStream(fo);
	byte[] buf = new byte[1024];
	int len = in.read(buf);
	while (len != -1) {
		out.write(buf, 0, len);
		len = in.read(buf);
	}
	out.close();
	fo.close();
	in.close();
}

public static void writePic(InputStream i, String Path) throws Exception {
	BufferedInputStream in = new BufferedInputStream(i);
	FileOutputStream fo = new FileOutputStream(Path);
	BufferedOutputStream out = new BufferedOutputStream(fo);
	byte[] buf = new byte[1024];
	int len = in.read(buf);
	while (len != -1) {
		out.write(buf, 0, len);
		len = in.read(buf);
	}
	out.close();
	fo.close();
	in.close();
}

 

 

5. 疑难问题解决

 

(1) 针对某些不能返回数据问题的解决方法

 

/**
 * 针对不能返回数据问题的POST提交
 * @param mainUrl 发票查询主页面URL
 * @param url 提交查询的URL
 * @param param
 * @param setCode
 * @param readCode
 * @return
 * 先到主页面访问一次,获取到COOKIES,将COOKIES设置到提交查询的请求中,在执行提交查询的访问。
 */
public static String doPost(String mainUrl, String url, NameValuePair[] param,
		String setCode, String readCode) {
	PostMethod method = new PostMethod(mainUrl);
	try {
		HttpClient httpClient = MyHttpClient.getHttpClient();
		int statusCode = httpClient.executeMethod(method);
		if (statusCode != HttpStatus.SC_OK) {
			return "查询失败!";
		}
		Cookie[] cookies = httpClient.getState().getCookies();
		httpClient.getState().addCookies(cookies);
	} catch (HttpException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		method.releaseConnection();
	}
	return doPost(url, param, setCode, readCode);
}

 

 (2) 针对不能获取到验证码图片的问题

 

/**
 * 针对有防盗链机制的网站获取验证码图片
 * @param list HTTP HEADER信息
 * @param url
 * @param path
 * @return
 */
public static void getYzm(List<Header> headers, String url, String path) {
	HttpClient httpClient = MyHttpClient.getHttpClient();
	httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);  
	download(url, path);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值