博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HTTP get、post 中请求json与map传参格式
阅读量:6128 次
发布时间:2019-06-21

本文共 7306 字,大约阅读时间需要 24 分钟。

hot3.png

import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.commons.lang3.StringUtils;import org.apache.http.HttpEntity;import org.apache.http.HttpStatus;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.config.RequestConfig;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.utils.URIBuilder;import org.apache.http.conn.HttpHostConnectException;import org.apache.http.entity.ContentType;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;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.alibaba.fastjson.JSON;public class HttpUtils {		private transient static Logger log = LoggerFactory.getLogger(HttpUtils.class);			public static String doGet(String url, Map
param){ //创建HttpClient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse httpResponse = null; try { //创建uri URIBuilder builder = new URIBuilder(url); if(param!=null && !param.isEmpty()){ for(String key :param.keySet()){ builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); // 创建httpGet请求 HttpGet httpGet = new HttpGet(uri); // 开始执行http请求 long startTime = System.currentTimeMillis(); httpResponse = httpclient.execute(httpGet); long endTime = System.currentTimeMillis(); // 获得响应状态码 int statusCode = httpResponse.getStatusLine().getStatusCode(); log.info("调用API花费时间(单位:毫秒):" + (endTime - startTime)); // 取出应答字符串 HttpEntity httpEntity = httpResponse.getEntity(); resultString = EntityUtils.toString(httpEntity,Charset.forName("UTF-8")); // 去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格 resultString.replaceAll("\r", ""); // 判断返回状态是否为200 if (statusCode != HttpStatus.SC_OK) { throw new RuntimeException(String.format("\n\tStatus:%s\n\tError Message:%s", statusCode,resultString)); } } catch (ClientProtocolException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } catch (URISyntaxException e) { log.error(e.getMessage(), e); } finally{ try { if(httpResponse != null){ httpResponse.close(); } httpclient.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } return resultString; } public static String doGet(String url){ return doGet(url,null); } public static String doPost(String url, Map
param) throws HttpHostConnectException,IOException{ //创建HttpClient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse httpResponse = null; try { // 创建HttpPost对象 HttpPost httpPost = new HttpPost(url); if(param!=null && !param.isEmpty()){ List
params = new ArrayList
(); for(String key :param.keySet()){ params.add(new BasicNameValuePair(key, param.get(key)==null?"":param.get(key).toString())); } httpPost.setEntity(new UrlEncodedFormEntity(params, Charset.forName("UTF-8"))); } //设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build(); httpPost.setConfig(requestConfig); // 开始执行http请求 long startTime = System.currentTimeMillis(); httpResponse = httpclient.execute(httpPost); long endTime = System.currentTimeMillis(); // 获得响应状态码 int statusCode = httpResponse.getStatusLine().getStatusCode(); log.info("调用API花费时间(单位:毫秒):" + (endTime - startTime)); // 取出应答字符串 HttpEntity httpEntity = httpResponse.getEntity(); resultString = EntityUtils.toString(httpEntity,Charset.forName("UTF-8")); // 判断返回状态是否为200 if (statusCode != HttpStatus.SC_OK) { throw new RuntimeException(String.format("\n\tStatus:%s\n\tError Message:%s", statusCode,resultString)); } //对返回结果进行非空校验 if (StringUtils.isBlank(resultString)||StringUtils.equals(StringUtils.trim(resultString), "null")) { log.error("被调用系统响应参数:{}",resultString); resultString = "{'code':100005,'msg':'响应不合法!'}"; } } catch (ClientProtocolException e) { log.error(e.getMessage(), e); } finally{ try { if(httpResponse != null){ httpResponse.close(); } httpclient.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } return resultString; } public static String doJsonPost(String url, Map
param) { String resultString = ""; if (param != null && !param.isEmpty()) { String json = JSON.toJSONString(param); resultString = doJsonPost(url, json); } else { resultString = doJsonPost(url, ""); } return resultString; } public static String doJsonPost(String url, String json) { // 创建HttpClient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse httpResponse = null; try { // 创建HttpPost对象 HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); // 开始执行http请求 long startTime = System.currentTimeMillis(); httpResponse = httpclient.execute(httpPost); long endTime = System.currentTimeMillis(); // 获得响应状态码 int statusCode = httpResponse.getStatusLine().getStatusCode(); log.info("调用API 花费时间(单位:毫秒):" + (endTime - startTime)); // 取出应答字符串 HttpEntity httpEntity = httpResponse.getEntity(); resultString = EntityUtils.toString(httpEntity, Charset.forName("UTF-8")); // 判断返回状态是否为200 if (statusCode != HttpStatus.SC_OK) { throw new RuntimeException(String.format( "\n\tStatus:%s\n\tError Message:%s", statusCode, resultString)); } } catch (ClientProtocolException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } finally { try { if (httpResponse != null) { httpResponse.close(); } httpclient.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } return resultString; } }

 

转载于:https://my.oschina.net/lisc2016/blog/2250561

你可能感兴趣的文章
第k小数
查看>>
构建之法阅读笔记三
查看>>
写给对前途迷茫的朋友:五句话定会改变你的人生
查看>>
并行程序设计学习心得1——并行计算机存储
查看>>
JAVA入门到精通-第86讲-半双工/全双工
查看>>
bulk
查看>>
js document.activeElement 获得焦点的元素
查看>>
C++ 迭代器运算
查看>>
【支持iOS11】UITableView左滑删除自定义 - 实现多选项并使用自定义图片
查看>>
day6-if,while,for的快速掌握
查看>>
JavaWeb学习笔记(十四)--JSP语法
查看>>
【算法笔记】多线程斐波那契数列
查看>>
java8函数式编程实例
查看>>
jqgrid滚动条宽度/列显示不全问题
查看>>
在mac OS10.10下安装 cocoapods遇到的一些问题
查看>>
angularjs表达式中的HTML内容,如何不转义,直接表现为html元素
查看>>
css技巧
查看>>
Tyvj 1728 普通平衡树
查看>>
[Usaco2015 dec]Max Flow
查看>>
javascript性能优化
查看>>