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/ ...
随机推荐
- zookerper安装使用教程
转载自 http://blog.java1234.com/blog/articles/379.html 再安装zookeeper之前,我们看下zookeeper简介 https://baike.bai ...
- Vue-webpack-hbuilderx 开发前端基本命令
--创建Vue 项目 pc 需要装 node 环境 ,安装完之后,就可以在cmd中使用npm 命令了 1:npm install -g vue-cli //电脑端需要安装vue 脚手架模板,电脑端一 ...
- ubuntu16.04安装openssh中报错解决
在使用apt-get直接进行安装时会报错: sudo apt-get install openssh-server 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态 ...
- ETL讲解
ETL是将业务系统的数据经过抽取.清洗转换之后加载到数据仓库的过程,目的是将企业中的分散.零乱.标准不统一的数据整合到一起,为企业的决策提供分析依据. ETL是BI项目重要的一个环节. 通常情况下,在 ...
- Linux 系统安全相关
本篇关于Linux的一些安全知识,主要就是与账号相关的安全. 账户文件锁定 当服务器中的用户账号已经固定,不在进行更改,可锁定账户文件.锁定后,无法添加.删除账号,也无法更改密码等. 锁定账户文件 c ...
- EFK(Elasticsearch+Filebeat+Kibana)收集容器日志
介绍 Elasticsearch 是一个实时的.分布式的可扩展的搜索引擎,允许进行全文.结构化搜索,它通常用于索引和搜索大量日志数据,也可用于搜索许多不同类型的文档. Beats 是数据采集的得力工具 ...
- uuid简述
什么是UUID? UUID是Universally Unique Identifier的缩写,它是在一定的范围内(从特定的名字空间到全球)唯一的机器生成的标识符,参考RFC规范-RFC4122. UU ...
- 洛谷P4213(杜教筛)
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 3e6 + 3; ...
- steam游戏存档迁移
之前玩的盗版guacamelee等着打折入正,今天入了,不想重新打了,就把存档从盗版迁移了一下. 盗版的目录是F:\Guacamelee\Profile\ALI213\Saves,该目录下又一个SAV ...
- WinForm 捕获异常 Application.ThreadException + AppDomain.CurrentDomain.UnhandledException
WinForm 捕获未处理的异常,可以使用Application.ThreadException 和AppDomain.CurrentDomain.UnhandledException事件 WinF ...