HttpClient 发送请求和参数
发送请求 没有参数
private static void getData() {
String timeStamp = String.valueOf(System.currentTimeMillis());
String code = "1234";
String key = "1234567";
String sign = DigestUtils.md5Hex(key + timeStamp + code).toUpperCase();
String url = "http://test/api/Information/getData?ts=" + timeStamp + "&code=" + code + "&sign=" + sign;
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try {
responseBody = closeableHttpClient.execute(httpPost, responseHandler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(responseBody);
// JSONObject response = new JSONObject(responseBody);
// JSONArray resultArray = response.getJSONArray("result");
// for (int i = 0; i < resultArray.length(); i++) {
// System.out.println(resultArray.getJSONObject(i));
// }
// System.out.println("/n/n" +responseBody);
}
发送请求 有参数
private static void syncData(String sampleId, String productId, String orderId, int baResult, int isPresentation) {
String timeStamp = String.valueOf(System.currentTimeMillis());
String code = "1234";
String key = "1234567";
// 添加 Http post 参数
List<String> jsonList = new ArrayList<String>();
JSONObject jsonObject = new JSONObject();
jsonObject.put("sampleId", sampleId); // 参数
jsonObject.put("productId", productId); // 参数
jsonObject.put("orderId", orderId); // 参数
jsonObject.put("baResult", baResult); // 参数
jsonObject.put("isPresentation", isPresentation); // 参数
String json = jsonObject.toString();
jsonList.add(json); // 把 json 放到集合里
String sign = DigestUtils.md5Hex(key + timeStamp + code + jsonList.toString()).toUpperCase();
String url = "http://test/api/Information/syncData?ts=" + timeStamp + "&code=" + code + "&sign=" + sign;
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new StringEntity(jsonList.toString()));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try {
responseBody = closeableHttpClient.execute(httpPost, responseHandler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(responseBody);
}
发送请求 上传pdf文件
private static void uploadPresentation(String productId, String orderId, String reportFile) {
File file = new File(reportFile);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.addBinaryBody("file", file); // 上传的 pdf 报告
multipartEntityBuilder.addTextBody("productId", productId); // 参数
multipartEntityBuilder.addTextBody("orderId", orderCode); // 参数
HttpEntity httpEntity = multipartEntityBuilder.build();
String timeStamp = String.valueOf(System.currentTimeMillis());
String code = "1234";
String key = "1234567";
String sign = DigestUtils.md5Hex(key + timeStamp + code).toUpperCase();
String url = "http://test/Information/uploadfile?ts=" + timeStamp + "&code=" + code + "&sign=" + sign;
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(httpEntity);
} catch (Exception e1) {
e1.printStackTrace();
}
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try {
responseBody = closeableHttpClient.execute(httpPost, responseHandler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(responseBody);
}
HttpClient 发送请求和参数的更多相关文章
- 使用HttpClient发送请求、接收响应
使用HttpClient发送请求.接收响应很简单,只要如下几步即可. 1.创建HttpClient对象. CloseableHttpClient httpclient = HttpClients.c ...
- .NetCore HttpClient发送请求的时候为什么自动带上了一个RequestId头部?
奇怪的问题 最近在公司有个系统需要调用第三方的一个webservice.本来调用一个下很简单的事情,使用HttpClient构造一个SOAP请求发送出去拿到XML解析就是了. 可奇怪的是我们的请求在运 ...
- 使用HttpClient发送请求接收响应
1.一般需要如下几步:(1) 创建HttpClient对象.(2)创建请求方法的实例,并指定请求URL.如果需要发送GET请求,创建HttpGet对象:如果需要发送POST请求,创建HttpPost对 ...
- httpClient 发送请求后解析流重用的问题(HttpEntity的重用:BufferedHttpEntity)
使用场景: 项目中使用httpClient发送一次http请求,以流的方式处理返回结果,开始发现返回的流只能使用一次,再次使用就会出错,后来看了一些解决方案,EntityUtils.consume(r ...
- httpclient发送不带参数post数据
两个问题: 1.httpclient怎样发送一个没有不论什么參数的post数据呢? 2.Webproject怎样去接收一个无參数的post呢? 起因: 今天(2014.1 ...
- 记录下httpclient 发送请求 服务端用@RequestBody 自动接收参数 报415
注解是post方式,那么检查以下内容:1. 你是否用了post请求2. 请求是否发送了数据3. 请求内容格式需要是 application/json .jquery 设置 contentType,-- ...
- httpclient post请求带参数返回数据乱码问题解决
客户端代码: //带参数的post请求 @Test public void doPostWithParam() throws Exception { CloseableHttpClient httpC ...
- httpclient发送请求的几种方式
package asi; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; ...
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
随机推荐
- python安装和pycharm安装与笔记
目录 计算机的基础知识 python安装和使用 pycharm安装和使用 [TOC] 计算机的基础知识 计算机是由什么组成的 cpu-----大脑 主板----身体 电源----心脏 内存----临时 ...
- poi读取excel的列和删除列
(各自根据具体的poi版本进行相应的替换即可) package com.br.loan.strategy.common.utils; import lombok.extern.slf4j.Slf4j; ...
- React: webpack模块组织关系
现代前端开发离不开打包工具,以 webpack 为代表的打包工具已经成为日常开发必备之利器,拿 React 技术栈为例,我们 ES6 形式的源代码,需要经过 webpack 和 Babel 处理,才能 ...
- Node: 通过Console打印日志 (Log Message via Console)
In normal development, we are likely to use 'console.log' for message logging, yet it's simple, we a ...
- githe和github连接,上传
Git入门 如果你完全没有接触过Git,你现在只需要理解通过Git的语法(敲入一些命令)就可以将代码上传到远程的仓库或者下载到本地的仓库(服务器),可知我们此时应该有两个仓库,就是两个放代码的地方,一 ...
- dedeCMS 两个站共用同一个数据库 图片路径统一
1 . 在 /include/extend.fun.php 中增加方法: function replaceurl($newurl){ $newurl=str_replace('src="/ ...
- influxDB应用及TICK stack
InfluxData平台用于处理度量和事件的时间序列平台,常被称为TICK stack,包含4个组件:Telegraf,influxDB,Chronograf和Kapacitor,分别负责时间序列数据 ...
- Redis开发与运维学习笔记
<Redis开发与运维>读书笔记 一.初始Redis 1.Redis特性与优点 速度快.redis所有数据都存放于内存:是用C语言实现,更加贴近硬件:使用了单线程架构,避免了多线程竞争 ...
- Synchronized关键字和锁升级,详细分析偏向锁和轻量级锁的升级
原文链接:https://blog.csdn.net/tongdanping/article/details/79647337 1.锁升级锁的4中状态:无锁状态.偏向锁状态.轻量级锁状态.重量级锁状态 ...
- Uva1349Optimal Bus Route Design(二分图最佳完美匹配)(最小值)
题意: 给定n个点的有向图问,问能不能找到若干个环,让所有点都在环中,且让权值最小,KM算法求最佳完美匹配,只不过是最小值,所以把边权变成负值,输出时将ans取负即可 这道题是在VJ上交的 #incl ...