第一种:需要httpclient的依赖包

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>

直接上代码:

public static String callBgrsjk(String requestParams) {
String url = null;
JSONObject jb=new JSONObject();
jb.put("code",0);
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(300 * 1000)
.setConnectTimeout(300 * 1000)
.build();
url = "http://URL:Port/地址";
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
post.setHeader("Content-Type","application/json;charset=utf-8");
StringEntity postingString = new StringEntity(requestParams,
"utf-8");
post.setEntity(postingString);
HttpResponse response = httpClient.execute(post);
String content = EntityUtils.toString(response.getEntity());
System.out.println(content);
return content;
} catch (SocketTimeoutException e) {
LoggerUtil.error("调用Dat+"
+ ".aService接口超时,超时时间:" + 300
+ "秒,url:" + url + ",参数:" + requestParams, e);
return jb.toString();
} catch (Exception e) {
LoggerUtil.error("调用DataService接口失败,url:" + url + ",参数:" + requestParams,
e);
return jb.toString();
}
}

第二种:使用jdk中的URL

/**
* 发送Http post请求
*
* @param xmlInfo
* json转化成的字符串
* @param URL
* 请求url
* @return 返回信息
*/
public static String doHttpPost(String xmlInfo, String URL) {
System.out.println("发起的数据:" + xmlInfo);
byte[] xmlData = xmlInfo.getBytes();
InputStream instr = null;
java.io.ByteArrayOutputStream out = null;
try {
URL url = new URL(URL);
URLConnection urlCon = url.openConnection();
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setUseCaches(false);
urlCon.setRequestProperty("content-Type", "application/json");
urlCon.setRequestProperty("charset", "utf-8");
urlCon.setRequestProperty("Content-length",
String.valueOf(xmlData.length));
System.out.println(String.valueOf(xmlData.length));
DataOutputStream printout = new DataOutputStream(
urlCon.getOutputStream());
printout.write(xmlData);
printout.flush();
printout.close();
instr = urlCon.getInputStream();
byte[] bis = IOUtils.toByteArray(instr);
String ResponseString = new String(bis, "UTF-8");
if ((ResponseString == null) || ("".equals(ResponseString.trim()))) {
System.out.println("返回空");
}
System.out.println("返回数据为:" + ResponseString);
return ResponseString; } catch (Exception e) {
e.printStackTrace();
return "0";
} finally {
try {
if(out!=null){
out.close();
}
if(instr!=null){
instr.close();
}
} catch (Exception ex) {
return "0";
}
}
}

第三种:使用apache的commons包

 /**
* 发送post请求
*
* @param params
* 参数
* @param requestUrl
* 请求地址
* @param authorization
* 授权书
* @return 返回结果
* @throws IOException
*/
public static String sendPost(String params, String requestUrl,
String authorization) throws IOException { byte[] requestBytes = params.getBytes("utf-8"); // 将参数转为二进制流
HttpClient httpClient = new HttpClient();// 客户端实例化
PostMethod postMethod = new PostMethod(requestUrl);
//设置请求头Authorization
//postMethod.setRequestHeader("Authorization", "Basic " + authorization);
// 设置请求头 Content-Type
postMethod.setRequestHeader("Content-Type", "application/json");
InputStream inputStream = new ByteArrayInputStream(requestBytes, 0,
requestBytes.length);
RequestEntity requestEntity = new InputStreamRequestEntity(inputStream,
requestBytes.length, "application/json; charset=utf-8"); // 请求体
postMethod.setRequestEntity(requestEntity);
httpClient.executeMethod(postMethod);// 执行请求
InputStream soapResponseStream = postMethod.getResponseBodyAsStream();// 获取返回的流
byte[] datas = null;
try {
datas = readInputStream(soapResponseStream);// 从输入流中读取数据
} catch (Exception e) {
e.printStackTrace();
}
String result = new String(datas, "UTF-8");// 将二进制流转为String
// 打印返回结果
// System.out.println(result); return result; }

HttpPost方式调用接口的3种方式的更多相关文章

  1. Python调用接口的几种方式

    1. requests import requests, jsongithub_url = 'https://api.github.com/user/repos'data = json.dumps({ ...

  2. C#动态调用WCF接口,两种方式任你选。

    写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...

  3. Python调用API接口的几种方式 数据库 脚本

    Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...

  4. Python调用API接口的几种方式

    Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...

  5. 动态调用WebService接口的几种方式

    一.什么是WebService? 这里就不再赘述了,想要了解的====>传送门 二.为什么要动态调用WebService接口? 一般在C#开发中调用webService服务中的接口都是通过引用过 ...

  6. 05 . Vue前端交互,fetch,axios,以asyncawait方式调用接口使用及案例

    目标 /* 1. 说出什么是前后端交互模式 2. 说出Promise的相关概念和用法 3. 使用fetch进行接口调用 4. 使用axios进行接口调用 5. 使用asynnc/await方式调用接口 ...

  7. HttpClient方式调用接口的实例

    使用HttpClient的方式调用接口的实例. public class TestHttpClient { public static void main(String[] args) { // 请求 ...

  8. 调用awk的三种方式

    调用awk的三种方式 调用awk有三种方式,一种为Shell命令行方式,另外两种是将awk程序写入脚本文件,然后执行该脚本文件.三种方式的命令格式归纳如下: 一.在Shell命令行输入命令调用awk, ...

  9. 调用init方法 两种方式 一个是浏览器方法 一个是 xml中手工配置(load-on-startup)

    调用init方法 两种方式 一个是浏览器方法 一个是 xml中手工配置(load-on-startup)

随机推荐

  1. ELK 日志采集 实战教程

    概要 带着问题去看教程: 不是用logstash来监听我们的日志,我们可以使用logback配置来使用TCP appender通过TCP协议将日志发送到远程Logstash实例. 我们可以使用Logs ...

  2. JavaScript 的 4 种数组遍历方法: for VS forEach() VS for/in VS for/of

    我们有多种方法来遍历 JavaScript 的数组或者对象,而它们之间的区别非常让人疑惑.Airbnb 编码风格禁止使用 for/in 与 for/of,你知道为什么吗? 这篇文章将详细介绍以下 4 ...

  3. 前端入门17-JavaScript进阶之作用域

    声明 本系列文章内容全部梳理自以下几个来源: <JavaScript权威指南> MDN web docs Github:smyhvae/web Github:goddyZhao/Trans ...

  4. es6 常用方法

    来自 https://www.cnblogs.com/lhl66/p/9555903.html 侵删 来自 https://www.cnblogs.com/lhl66/p/8862106.html 侵 ...

  5. 小tips:JS之for in、Object.keys()和Object.getOwnPropertyNames()的区别

    for..in循环 使用for..in循环时,返回的是所有能够通过对象访问的.可枚举的属性,既包括存在于实例中的属性,也包括存在于原型中的实例.这里需要注意的是使用for-in返回的属性因各个浏览器厂 ...

  6. Catalan卡特兰数入门

    简介 卡特兰数是组合数学中的一种常见数列 它的前几项为: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, ...

  7. 从.Net到Java学习第十二篇——SpringBoot+JPA提供跨域接口

    从.Net到Java学习系列目录 最近又撸了半个月的前端代码,做app离线存储,然后又花了一周去将过去的wcf项目转webapi,java又被落下了,总感觉我特么像斗地主中的癞子牌,变来变去..... ...

  8. [Android framework学习] ViewGroup的addView函数分析

    博客首页:http://www.cnblogs.com/kezhuang/p/ Android中整个的View的组装是采用组合模式. ViewGroup就相当与树根,各种Layout就相当于枝干,各种 ...

  9. ext图片预览功能实现,前端代码

    效果图: extjs代码: // 模型 Ext.define('ParkingAttachment', {extend: "Ext.data.Model", idProperty: ...

  10. MongoDB的常用命令和增查改删

    数据库操作 Mongodb MySQL 查询库 show databases | show dbs show databases 选中库 use databaseName use databaseNa ...