java发送soap报文_java代码请求SOAP

本文展示了如何使用Java进行SOAP请求的代码实现,包括创建HttpClient、设置请求超时时间、添加SOAP消息头以及执行POST请求。通过HttpPostToSoap类的doPostSoap方法,可以将SOAP XML消息发送到指定的WebService接口,并获取响应内容。
摘要由CSDN通过智能技术生成

1 package com.http.soap;

2

3 import java.io.BufferedReader;

4 import java.io.IOException;

5 import java.io.InputStreamReader;

6 import java.nio.charset.Charset;

7

8 import org.apache.http.HttpEntity;

9 import org.apache.http.client.ClientProtocolException;

10 import org.apache.http.client.config.RequestConfig;

11 import org.apache.http.client.methods.CloseableHttpResponse;

12 import org.apache.http.client.methods.HttpPost;

13 import org.apache.http.entity.StringEntity;

14 import org.apache.http.impl.client.CloseableHttpClient;

15 import org.apache.http.impl.client.HttpClientBuilder;

16 import org.apache.http.util.EntityUtils;

17

18 public class HttpPostToSoap {

19 static int socketTimeout = 10000;// 请求超时时间

20 static int connectTimeout = 10000;// 传输超时时间

21 static final String soap = "text/xml;charset=UTF-8";

22 static final String soap11 = "application/soap+xml;charset=UTF-8";

23

24 /**

25 * 同步HttpPost请求发送SOAP格式的消息

26 *

27 * @param webServiceURL

28 * WebService接口地址

29 * @param soapXml

30 * 消息体

31 * @param soapAction

32 * soapAction

33 * @param soapType

34 * soap版本

35 * @return

36 */

37 public static String doPostSoap(String webServiceURL, String soapXml,String soapAction, String soapType) {

38 // BufferedReader reader = null;

39 // 创建HttpClient

40 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

41 CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

42 // 创建Post请求

43 HttpPost httpPost = new HttpPost(webServiceURL);

44 // 设置请求和传输超时时间

45 RequestConfig requestConfig = RequestConfig.custom()

46 .setSocketTimeout(socketTimeout)

47 .setConnectTimeout(connectTimeout).build();

48 httpPost.setConfig(requestConfig);

49 // 设置Post请求报文头部

50 httpPost.setHeader("Content-Type", soapType);

51 httpPost.setHeader("SOAPAction", soapAction);

52 // 添加报文内容

53 StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));

54 httpPost.setEntity(data);

55 try {

56 // 执行请求获取返回报文

57 CloseableHttpResponse response = closeableHttpClient.execute(httpPost);

58 HttpEntity httpEntity = response.getEntity();

59 if (httpEntity != null) {

60 // 打印响应内容

61 return EntityUtils.toString(httpEntity, "UTF-8");

62 }

63 // if (httpEntity != null) {

64 // reader = new BufferedReader(new InputStreamReader(httpEntity .getContent(), "UTF-8"));

65 // StringBuffer sb = new StringBuffer();

66 // String line = null;

67 // while ((line = reader.readLine()) != null) {

68 // sb.append(line);

69 // sb.append("\r\n");

70 // }

71 // return sb.toString();

72 // }

73 // 释放资源

74 closeableHttpClient.close();

75 } catch (ClientProtocolException e) {

76 e.printStackTrace();

77 } catch (IOException e) {

78 e.printStackTrace();

79 }

80 return null;

81 }

82 }

测试

package com.http.soap;

public class TestSoap {

public static void main(String[] args) {

String soapXml=“请求报文”;

String result=HttpToSoap.doPostSoap("http://192.168.18.161:8080/mtosi", soapXml, "", HttpToSoap.soap);

System.out.println(result);

}

}

标签:java,String,代码,http,static,org,apache,import,SOAP

来源: https://www.cnblogs.com/lhq1996/p/13035643.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值