java httpclient4.5_httpclient4.5使用详解 httpclient 4.5 post传递json参数

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

import org.apache.http.NameValuePair;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.methods.HttpUriRequest;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

/**

* 工具类 httpclient4.5

*

* @author xq

*

*/

public class HttpclientUtils {

/**

* post请求 json参数

*

* @param url

* @param bodyJsonParams

* @param headers

* @return

* @throws IOException

*/

public static String doPost(String url, String bodyJsonParams, Map headers) throws IOException {

HttpPost httpPost = new HttpPost(url);

httpPost.addHeader("Content-Type", "application/json");

httpPost.setEntity(new StringEntity(bodyJsonParams));

if (headers != null && headers.keySet().isEmpty()) {

Set keySet = headers.keySet();

Iterator iterator = keySet.iterator();

while (iterator.hasNext()) {

String key = iterator.next();

String value = headers.get(key);

httpPost.addHeader(key, value);

}

}

return execute(httpPost);

}

/**

* post k-v参数

*

* @param url

* @param params

* @param headers

* @return

* @throws IOException

*/

public static String doPost(String url, Map params, Map headers)

throws IOException {

HttpPost httpPost = new HttpPost(url);

if (params != null && params.keySet().isEmpty()) {

List list = new ArrayList<>();

Set keySet = headers.keySet();

Iterator iterator = keySet.iterator();

while (iterator.hasNext()) {

String key = iterator.next();

String value = headers.get(key);

list.add(new BasicNameValuePair(key, value));

}

httpPost.setEntity(new UrlEncodedFormEntity(list));

}

if (headers != null && headers.keySet().isEmpty()) {

Set keySet = headers.keySet();

Iterator iterator = keySet.iterator();

while (iterator.hasNext()) {

String key = iterator.next();

String value = headers.get(key);

httpPost.addHeader(key, value);

}

}

return execute(httpPost);

}

/**

* get请求

*

* @param url

* @param params

* @param headers

* @return

* @throws IOException

* @throws ClientProtocolException

*/

public static String doGet(String url, Map params, Map headers) throws IOException {

// 参数

StringBuilder paramsBuilder = new StringBuilder(url);

if (params != null && params.keySet().isEmpty()) {

if (url.indexOf("?") == -1) {

paramsBuilder.append("?");

}

List list = new ArrayList<>();

Set keySet = headers.keySet();

Iterator iterator = keySet.iterator();

while (iterator.hasNext()) {

String key = iterator.next();

String value = headers.get(key);

list.add(new BasicNameValuePair(key, value));

}

String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(list));

paramsBuilder.append(paramsStr);

}

HttpGet httpGet = new HttpGet(paramsBuilder.toString());

// 头

if (headers != null && headers.keySet().isEmpty()) {

Set keySet = headers.keySet();

Iterator iterator = keySet.iterator();

while (iterator.hasNext()) {

String key = iterator.next();

String value = headers.get(key);

httpGet.addHeader(key, value);

}

}

return execute(httpGet);

}

/**

* 执行请求并返回string值

*

* @param httpUriRequest

* @return

* @throws IOException

*/

private static String execute(HttpUriRequest httpUriRequest) throws IOException {

try (CloseableHttpClient httpClient = HttpClients.createDefault()) {

CloseableHttpResponse response = httpClient.execute(httpUriRequest);

if (response.getStatusLine().getStatusCode() == 200) {// 请求成功状态

try (BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(response.getEntity().getContent()))) {

String result="";

String tmp=null;

while((tmp=bufferedReader.readLine())!=null){

result+=tmp;

}

return result;

}

}

}

return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值