soap get/post请求
pom.xml依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
public static String getSoapRequest(String getUrl, String soapXml, String soapAction, String userName, String password) throws UnsupportedEncodingException {
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// 设置BasicAuth
CredentialsProvider provider = new BasicCredentialsProvider();
// Create the authentication scope
AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
// Create credential pair,在此处填写用户名和密码
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
// Inject the credentials
provider.setCredentials(scope, credentials);
// Set the default credentials provider
httpClientBuilder.setDefaultCredentialsProvider(provider);
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpGet httpGet = new HttpGet(getUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
httpGet.setConfig(requestConfig);
try {
// httpGet.setHeader("Content-Type", "text/xml;charset=UTF-8");
StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
httpGet.setHeader("SOAPAction", soapAction);
CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
logger.info("response:" + retStr);
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
logger.error("exception in doGetRequest", e);
}
return retStr;
}
post请求:
static int socketTimeout = 30000;// 请求超时时间
static int connectTimeout = 30000;// 传输超时时间
static Logger logger = Logger.getLogger(HttpClientSoapUtil.class); /**
* 使用SOAP1.1发送消息,可调1.1,也可调用1.2
*/
public static String doPostSoap1_1WithBasicAuth(String postUrl, String soapXml, String soapAction, String userName, String password) {
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// 设置BasicAuth
CredentialsProvider provider = new BasicCredentialsProvider();
// Create the authentication scope
AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
// Create credential pair,在此处填写用户名和密码
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
// Inject the credentials
provider.setCredentials(scope, credentials);
// Set the default credentials provider
httpClientBuilder.setDefaultCredentialsProvider(provider);
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
logger.info("response:" + retStr);
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
logger.error("exception in doPostSoap1_1", e);
}
return retStr;
}
soap get/post请求的更多相关文章
- java使用POST发送soap报文请求webservice返回500错误解析
本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据, 对错误Server returned HTTP ...
- WebService Get/Post/Soap 方式请求
import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.InputStream; im ...
- 随便聊聊 SOA & SOAP & WebService 的一些东西,以及客户端开发的代码逻辑解析
http://blog.csdn.net/hikaliv/article/details/6459779 一天的时间调通了一个 WebService 的 Java 端的 C/S.一个 Android ...
- SOAP和WSDL的一些必要知识
SOAP和WSDL对Web Service.WCF进行深入了解的基础,因此花一些时间去了解一下是很有必要的. 一.SOAP(Simple Object Access Protocol) 如果我们要调用 ...
- Web Services 中XML、SOAP和WSDL的一些必要知识
Web Services 是由xml来定义数据格式的,通过SOAP协议在各个系统平台中传输,那么接下来讨论下SOAP和WSDL的各自作用. SOAP和WSDL对Web Service.WCF进行深入了 ...
- SOAP和WSDL的一些必要知识(转)
原文地址:SOAP和WSDL的一些必要知识 SOAP和WSDL对Web Service.WCF进行深入了解的基础,因此花一些时间去了解一下是很有必要的. 一.SOAP(Simple Object Ac ...
- php中soap的使用实例以及生成WSDL文件,提供自动生成WSDL文件的类库——SoapDiscovery.class.php类
1. web service普及: Webservice soap wsdl区别之个人见解 Web Service实现业务诉求: Web Service是真正“办事”的那个,提供一种办事接口的统称. ...
- PHP使用SOAP调用.net的WebService数据
需要和一个.net系统进行数据交换,对方提供了一个WebService接口,使用PHP如何调用这个数据呢,下面就看看使用SOAP调用的方法吧 这个与一般的PHP POST或GET传值再查库拿数据的思路 ...
- C#使用SOAP调用Web Service
程序代码 using System;using System.IO;using System.Net;using System.Text; namespace ConsoleApplication1{ ...
随机推荐
- 【openmp】for循环的break问题
问题描述:在用openmp并行化处理for循环的时候,便无法在for循环中用break语句,那么我们如何实现这样的机制呢?在stackoverflow上看到一个不错的回答总结一下. volatile ...
- Netty基础系列(5) --零拷贝彻底分析
前言 上一节(堆外内存与零拷贝)当中我们从jvm堆内存的视角解释了一波零拷贝原理,但是仅仅这样还是不够的. 为了彻底搞懂零拷贝,我们趁热打铁,接着上一节来继续讲解零拷贝的底层原理. 感受一下NIO的速 ...
- R语言中如何找出在两个数据框中完全相同的行(How to find common rows between two dataframe in R?)
I would like to make a new data frame which only includes common rows of two separate data.frame. ex ...
- 关于sparksql中设置自定义自增列的相关要点(工作共踩过的坑-1)
小白终于进入了职场,从事大数据方面的工作! 分到项目组了,搬砖的时候遇到了一个这样的问题. 要求:用spark实现oracle的存储过程中计算部分. 坑:由于报表中包含了一个ID字段,其要求是不同的区 ...
- go 学习笔记之go是不是面向对象语言是否支持面对对象编程?
面向对象编程风格深受广大开发者喜欢,尤其是以 C++, Java 为典型代表的编程语言大行其道,十分流行! 有意思的是这两中语言几乎毫无意外都来源于 C 语言,却不同于 C 的面向过程编程,这种面向对 ...
- Code signing is required for product type 'Unit Test Bundle' in SDK 'iOS 11.0.1'
Code signing is required for product type 'Unit Test Bundle' in SDK 'iOS 11.0.1' 进入 projects and lis ...
- 浅析runtime包中的三个方法Gosched、Goexit、GOMAXPROCS
Gosched 暂停当前goroutine,使其他goroutine先行运算.只是暂停,不是挂起,当时间片轮转到该协程时,Gosched()后面的操作将自动恢复 未使用Gosched的代码 packa ...
- C笔记_动态库和静态库
1. 静态库 创建 工程属性配置中设置为lib静态库,编辑.h文件和.c文件,生成即可. 使用 方法一: 添加工程的头文件目录:工程---属性---配置属性---c/c++---常规---附加包含目录 ...
- 关于简单递归在python3中的实现
话不多说,奉上代码: #倒计时 def count_down(i): if i <= 0: return else: print(str(i)) count_down(i - 1) #求阶乘 d ...
- TextCNN 代码详解(附测试数据集以及GitHub 地址)
前言:本篇是TextCNN系列的第三篇,分享TextCNN的优化经验 前两篇可见: 文本分类算法TextCNN原理详解(一) 一.textCNN 整体框架 1. 模型架构 图一:textCNN 模型结 ...