发送请求 没有参数

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 发送请求和参数的更多相关文章

  1. 使用HttpClient发送请求、接收响应

    使用HttpClient发送请求.接收响应很简单,只要如下几步即可. 1.创建HttpClient对象.  CloseableHttpClient httpclient = HttpClients.c ...

  2. .NetCore HttpClient发送请求的时候为什么自动带上了一个RequestId头部?

    奇怪的问题 最近在公司有个系统需要调用第三方的一个webservice.本来调用一个下很简单的事情,使用HttpClient构造一个SOAP请求发送出去拿到XML解析就是了. 可奇怪的是我们的请求在运 ...

  3. 使用HttpClient发送请求接收响应

    1.一般需要如下几步:(1) 创建HttpClient对象.(2)创建请求方法的实例,并指定请求URL.如果需要发送GET请求,创建HttpGet对象:如果需要发送POST请求,创建HttpPost对 ...

  4. httpClient 发送请求后解析流重用的问题(HttpEntity的重用:BufferedHttpEntity)

    使用场景: 项目中使用httpClient发送一次http请求,以流的方式处理返回结果,开始发现返回的流只能使用一次,再次使用就会出错,后来看了一些解决方案,EntityUtils.consume(r ...

  5. httpclient发送不带参数post数据

    两个问题:      1.httpclient怎样发送一个没有不论什么參数的post数据呢?      2.Webproject怎样去接收一个无參数的post呢? 起因:      今天(2014.1 ...

  6. 记录下httpclient 发送请求 服务端用@RequestBody 自动接收参数 报415

    注解是post方式,那么检查以下内容:1. 你是否用了post请求2. 请求是否发送了数据3. 请求内容格式需要是 application/json .jquery 设置 contentType,-- ...

  7. httpclient post请求带参数返回数据乱码问题解决

    客户端代码: //带参数的post请求 @Test public void doPostWithParam() throws Exception { CloseableHttpClient httpC ...

  8. httpclient发送请求的几种方式

    package asi; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; ...

  9. Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

随机推荐

  1. npm 安装、卸载 模块或包的命令(转载)

    npm安装卸载命令 来源:https://www.jianshu.com/p/e6ee00ea03cd npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目 ...

  2. Oracle 数据类型比较规则

    数值 较大的值被认为大于较小的值.所有负数都小于零,所有正数都小于零.因此,-1小于100:-100小于-1. 浮点值NaN(not a number))大于任何其他数值,且等于自身. 日期时间值 较 ...

  3. DNS 原理入门 - 阮一峰(转载)

      DNS 原理入门 作者: 阮一峰 日期: 2016年6月16日 DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 本文详细介绍DNS的原理,以及如何运用工具软件 ...

  4. CAD中如何将图形按一定的比例放大或缩小

    1.双击CAD快捷键图标,打开CAD绘图界面: 2.以正五边形为例,点击左边的正多边形按钮: 3.绘制好后得到五边形图形: 4.给图形做好尺寸标注方便直观比较: 5.选择图像在命令行输入sc命令,按键 ...

  5. 从linux进程角度看JVM内存模型

    普通进程栈区,在JVM一般仅仅用做线程栈,如下图所示 首先是永久代.永久代本质上是Java程序的代码区和数据区.Java程序中类(class),会被加载到整个区域的不同数据结构中去,包括常量池.域.方 ...

  6. MySQL/MariaDB数据库的复制监控和维护

      MySQL/MariaDB数据库的复制监控和维护 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.清理日志 1>.删除指定日志文件名称之前的日志(也可用基于时间) M ...

  7. MySQL/MariaDB数据库的查询缓存优化

    MySQL/MariaDB数据库的查询缓存优化 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL架构 Connectors(MySQL对外提供的交互接口,API): ...

  8. 关于struct和typedef struct

    以 struct TelPhone{ ]; ]; }; 为例 这里先定义了一个 TelPhone的结构体. 加入需要为TelPhone定义一个别名: 其语法为 typedef TelPhone TP: ...

  9. 2018年第十届ACMICPC四川省大学程序设计竞赛

    ..拿金了 没给学校丢脸 A ....SB题啊 比赛的时候都没看 裸的一个bitset前缀和 先开一个1e4*1e4的二维bitset数组 初始第i个数组的值为1 << i (即B[i]= ...

  10. python的gui库tkinter

    导入tkinter模块 import tkinter as tk 设置窗口名字和大小 frame=tk.Tk() frame.title('数学') frame.geometry('200x440') ...