soap get/post请求

本文介绍了一种使用HttpClient进行SOAP请求的方法,包括GET和POST请求,详细展示了如何设置Basic Auth认证,配置请求超时时间,以及如何处理响应。适用于需要与SOAP服务交互的开发人员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值