博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
soap get/post请求
阅读量:4548 次
发布时间:2019-06-08

本文共 3914 字,大约阅读时间需要 13 分钟。

 

pom.xml依赖:

 

org.apache.httpcomponents
httpclient
4.5.3

 

 

 

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; }

 

转载于:https://www.cnblogs.com/qq1141100952com/p/11114565.html

你可能感兴趣的文章
输入输出外挂(纯数字型)
查看>>
限制输出字数,超过的用...省略
查看>>
bnuoj25660 Two Famous Companies
查看>>
股票投资
查看>>
C# 启动另一个程序
查看>>
木桶效应
查看>>
springMVC3学习(二)--ModelAndView对象
查看>>
postgres前言(常用语句熟悉 系列一)
查看>>
Windows 上 GitHub Desktop 的操作[转]
查看>>
Leetcode-916. Word Subsets-(Medium)
查看>>
C# 解决无法识别的属性 configProtectionProvider
查看>>
js中的this
查看>>
ACM_三角形蛇形矩阵
查看>>
在IIS服务器上部署svg/woff/woff2字体
查看>>
计算机基础作业 网页制作
查看>>
HDU 4467 Graph
查看>>
并发容器之ConcurrentHashMap(转载)
查看>>
GUID和自增ID的比较_delete
查看>>
Mysql数据库如何自动备份
查看>>
javascript中如何获取对象名
查看>>