学习笔记:HttpClient的基本使用
筱米加步枪
posted @ 2010年7月26日 21:24
in [ 开源框架 ]
with tags
HttpClient
, 4664 阅读
之前公司要求做回报数据处理后结果发送给用户,用户端提供了一个Servlet用来接收数据。因此需要写一个基于Http协议的相关工具进行数据的传输,网上搜索了下,Java关于这方面的技术有两个:一是在JDK中的java.net包中已经提供了访问Http的基本功能,二是Apache Jakarta Common下的一个子项目开源API之HttpClient的子项目,提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包。我当然选择Apache的东西啦,于是搜索了相关材料,功能是实现了,不过对于这里面的一些东西还要加以再学习。
安装HttpClient 需要commons-codec-1.3.jar包、commons-httpclient-3.1.jar包、commons-logging-1.0.4.jar包
根据参考来的资料,自己写了一个基本的一个类,提供基本的功能,贴个代码:
/** * * <pre> * 关于Http协议进行操作,提供转发和访问网路地址的相关操作。 * </pre> * * @author 陈书挺 * @create 2010-7-23 * @version v1.0 * * <pre> * 修改时间 修改人 修改原因 * 2010-7-23 陈书挺 新建类 * </pre> * */ public class HttpOperator { //URL地址 private String url; /** * 根据URL参数创建构造函数<br> * 默认提交方式为POST提交方式 * @param url URL地址 */ public HttpOperator(String url){ this.url = url; } /** * 获取页面内容(页面代码),可指定提交方式<br> * 提交方式取值为: * SubmitMethod.GET :GET提交方式 * SubmitMethod.POST : POST 提交方式 * @param 提交方式 * @return 页面内容 * @throws IOException */ public String getPageContent(SubmitMethod submitMethod) throws IOException{ // 1.创建httpClient实例 HttpClient httpClient = new HttpClient(); if(submitMethod == SubmitMethod.GET){ //2.GetMethod会自动处理转发过程 GetMethod getMethod = new GetMethod(this.url); return this.getPageContentForGet(httpClient, getMethod); }else if(submitMethod == SubmitMethod.POST){ PostMethod postMethod = new PostMethod(this.url); return this.getPageContentForPost(httpClient, postMethod); }else{ return null; } } /** * 执行转发 可进行传参数key参数名,value指定参数值 * @param key 参数名 * @param value 参数值 * @return Http状态码 * @throws HttpException * @throws IOException */ public int executeForward(String key , String value) throws HttpException, IOException{ PostMethod postMethod = null; try{ HttpClient httpClient = new HttpClient(); postMethod = new PostMethod(url); postMethod.setParameter(key, value); int statusCode = httpClient.executeMethod(postMethod); return statusCode; }catch (Exception e){ throw new IOException(e); }finally{ postMethod.releaseConnection(); } } /** * 执行转发 可进行传参数集体参数map ,其中Map中的key指定参数名,value指定参数值 * @param map 参数Map对象 * @return Http状态码 * @throws HttpException * @throws IOException */ public int executeForwad(Map<String , String> map) throws HttpException, IOException{ NameValuePair [] nameValue = new NameValuePair[map.size()]; int k = 0; for(String key : map.keySet()){ nameValue[k].setName(key); nameValue[k++].setValue(map.get(key)); } return executeForward(nameValue); } /** * 执行转发 可进行传参数集体参数NameValuePair数组<br> * 其中NameValuePair对象封装了name 和value属性 * @param parameters NameValuePair对象 * @return Http状态码 * @throws HttpException * @throws IOException */ public int executeForward(NameValuePair[] parameters) throws HttpException, IOException { PostMethod postMethod = null; try { HttpClient httpClient = new HttpClient(); postMethod = new PostMethod(url); postMethod.setRequestBody(parameters); int statusCode = httpClient.executeMethod(postMethod); return statusCode; } catch (Exception e) { throw new IOException(e); } finally { postMethod.releaseConnection(); } } /** * 执行转发 将字节数组放入传输管道中进行传输数据 * @param data 要传输的数据 * @return Http状态码 * @throws HttpException * @throws IOException */ public int executeForward(byte[] data) throws HttpException, IOException { PostMethod postMethod = null; try { HttpClient httpClient = new HttpClient(); postMethod = new PostMethod(url); ByteArrayInputStream bis = new ByteArrayInputStream(data); InputStreamRequestEntity requestEntity = new InputStreamRequestEntity(bis); postMethod.setRequestEntity(requestEntity); int statusCode = httpClient.executeMethod(postMethod); return statusCode; } catch (Exception e) { throw new IOException(e); } finally { postMethod.releaseConnection(); } } /** * 执行转发 将字符串类型放入传输管道中进行传输数据 * @param data 要传输的数据 * @return Http状态码 * @throws HttpException * @throws IOException */ public int executeForward(String data) throws HttpException, IOException{ return executeForward(data.getBytes()); } /** * 执行转发 将输入流中的数据放入传输管道中进行传输数据 * @param inputStream 输入流 * @return Http状态码 * @throws HttpException * @throws IOException */ public int executeForward(InputStream inputStream) throws HttpException, IOException { PostMethod postMethod = null; try { HttpClient httpClient = new HttpClient(); postMethod = new PostMethod(url); InputStreamRequestEntity requestEntity = new InputStreamRequestEntity(inputStream); postMethod.setRequestEntity(requestEntity); int statusCode = httpClient.executeMethod(postMethod); return statusCode; } catch (Exception e) { throw new IOException(e); } finally { postMethod.releaseConnection(); } } /* * 使用GET方式获取页面内容 * @param httpClient HttpClient对象 * @param getMethod Get提交方式类 * @return 页面内容 * @throws IOException */ private String getPageContentForGet(HttpClient httpClient , GetMethod getMethod) throws IOException{ // 使用系统提供的默认恢复策略 重试3次 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { //3.调用httpClient实例来执行getMethod方法 //返回服务器返回的状态码 int statusCode = httpClient.executeMethod(getMethod); //如果执行成功 if(statusCode == HttpStatus.SC_OK){ //4.读取内容 byte [] responseBody = getMethod.getResponseBody(); return new String(responseBody); } } catch (HttpException e) { //1.协议不对 //2.返回内容不正常 //3.该异常是无法恢复的 throw new HttpException(e.getMessage()); } catch (IOException e) { //通常由于网络原因造成 //出现IO异常时候HttpClient会根据制定的恢复策略进行重试 //恢复策略可通过实现HttpMethodRetryHandler接口来实现 throw new IOException(e); } finally{ // 5.释放连接 getMethod.releaseConnection(); } return null; } /* * 使用POST方式获取页面内容 * @param httpClient HttpClient对象 * @param postMethod post提交方式类 * @return 页面内容 * @throws IOException */ private String getPageContentForPost(HttpClient httpClient , PostMethod postMethod) throws HttpException, IOException{ try{ int statusCode = httpClient.executeMethod(postMethod); if(statusCode == HttpStatus.SC_OK){ byte [] responseBody = postMethod.getResponseBody(); return new String(responseBody); } }catch(IOException e){ throw new IOException(e); }finally{ postMethod.releaseConnection(); } return null; } }