
static int socketTimeout = 300000;// 请求超时时间 |
static int connectTimeout = 300000;// 传输超时时间 |
/** |
* 使用SOAP1.1发送消息 |
* |
* @param postUrl |
* @param mapXml |
* @param soapAction |
* @return |
*/ |
public static String doPostSoap1_1(String postUrl, Map<String, Object> mapXml, |
String soapAction) { |
String retStr = ""; |
String xml = ""; |
// 创建HttpClientBuilder |
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); |
// 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 ); |
xml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.vshop.zjjzfy.com/\">\n" + |
" <soapenv:Header/>\n" + |
" <soapenv:Body>\n" + |
" <ser:" + soapAction + ">\n"; |
int i = 0; |
for ( String key : mapXml.keySet() ) { |
xml += " <arg" + i + "><![CDATA[" + mapXml.get( key ) + "]]></arg" + i + ">\n"; |
i++; |
} |
xml += " </ser:" + soapAction + ">\n" + |
" </soapenv:Body>\n" + |
"</soapenv:Envelope>"; |
System.out.println(xml); |
StringEntity data = new StringEntity( xml, 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"); |
} |
// 释放资源 |
closeableHttpClient.close(); |
} catch (Exception e) { |
System.out.println("exception in doPostSoap1_1" + e); |
} |
retStr = retStr.substring( retStr.indexOf("<return>") + 8, retStr.indexOf("</return>")); |
retStr = StringEscapeUtils.unescapeHtml( retStr ); |
return retStr; |
} |
public final static String POST_URL = "http://localhost:8080/services/ruirentang"; |
public static void main(String[] args) { |
//多参字符串xml请求 |
String pre_id = "1009022100"; |
String ter_id = "小票机"; |
String card_id = "6217002020036666666"; |
Map<String, Object> param = new HashMap<>(); |
param.put( "pre_id", pre_id ); |
param.put( "ter_id", ter_id ); |
param.put( "card_id", card_id ); |
String msg = doPostSoap1_1( POST_URL, param, "test" ); |
System.out.println(msg); |
//一参xml格式请求 |
Info p = new Info(); |
p.setPre_id("1009022100"); |
p.setTer_id("小票机"); |
p.setCard_id("6217002020036666666"); |
p.setCust_id("81900000994"); |
Infos infos = new Infos(); |
infos.setInfo(p); |
//把对象转化为 XML,并且设置别名 |
String xml = XStreamUtil.beanToXmlWithTag(infos); |
param = new HashMap<>(); |
param.put( "xml", xml ); |
//把对象转化为 XML,并且设置别名 |
msg = doPostSoap1_1( POST_URL, param, "jf_query" ); |
System.out.println(msg); |
} |



