HttpClient请求调用封装

编程技术  /  houtizong 发布于 3年前   69

1. maven依赖包下载

<!-- httpclient --> <dependency>   <groupId>org.apache.httpcomponents</groupId>   <artifactId>httpclient</artifactId>   <version>4.2.1</version>     </dependency>

 

2. HttpClientUtil.java

package com.snail.common.util;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.ResponseHandler;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.impl.client.BasicResponseHandler;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;/** * HttpClient调用工具类 *  * @author penghuaiyi * @date 2014-04-04 */public class HttpClientUtil {/** * 发送GET请求 * @param uri * @return */public static String sendGet(String uri) {String responseBody = null;HttpClient httpClient = new DefaultHttpClient();try {HttpGet httpGet = new HttpGet(uri);System.out.println("executing request " + httpGet.getURI());// Create a response handlerResponseHandler<String> responseHandler = new BasicResponseHandler();responseBody = httpClient.execute(httpGet, responseHandler);System.out.println("----------------------------------------");System.out.println(responseBody);System.out.println("----------------------------------------");} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {httpClient.getConnectionManager().shutdown();}return responseBody;}/** * 发送POST请求 * @param uri * @param paramMap 请求参数 * @return */public static String sendPost(String uri, Map<String, String> paramMap) {return sendPost(uri, paramMap, null);}/** * 发送POST请求 * @param uri * @param paramMap 请求参数 * @param charset 参数编码 * @return */public static String sendPost(String uri, Map<String, String> paramMap, String charset) {String responseBody = null;HttpClient httpClient = new DefaultHttpClient();try {HttpPost httpPost = new HttpPost(uri);System.out.println("executing request " + httpPost.getURI());if (paramMap != null) {List<NameValuePair> nvps = new ArrayList<NameValuePair>(paramMap.size());for (Map.Entry<String, String> entry : paramMap.entrySet()) {NameValuePair nvp = new BasicNameValuePair(entry.getKey(),entry.getValue());nvps.add(nvp);}if(charset!=null){httpPost.setEntity(new UrlEncodedFormEntity(nvps,charset));}else{httpPost.setEntity(new UrlEncodedFormEntity(nvps));}}// Create a response handlerResponseHandler<String> responseHandler = new BasicResponseHandler();responseBody = httpClient.execute(httpPost, responseHandler);System.out.println("----------------------------------------");System.out.println(responseBody);System.out.println("----------------------------------------");} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {httpClient.getConnectionManager().shutdown();}return responseBody;}}

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客