一、使用hutool的SoapClient,代码如下
1)相关引用
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.11</version>
<scope>compile</scope>
</dependency>
2)实现代码
package com.lqt.test;
import cn.hutool.core.lang.Console;
import cn.hutool.http.webservice.SoapClient;
import java.io.IOException;
import java.util.HashMap;
public class WebServiceTest {
public static void main(String[] args) throws IOException {
String url = "http://ip:端口号/soap/IPacsServices";
HashMap<String, Object> map = new HashMap<>();
map.put("OrderNo", "111111");
map.put("PatName", "test");
SoapClient client = SoapClient.create(url)
// 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
.setMethod("queryQueue", "urn:PacsServicesIntf-IPacsServices")
// 设置参数,此处自动添加方法的前缀
.setParams(map);
// 发送请求,参数true表示返回一个格式化后的XML内容
// 返回内容为XML字符串,可以配合XmlUtil解析这个响应
Console.log(client.send(true));
}
}
package com.lqt.test;
import cn.hutool.http.webservice.SoapClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
public class WebServiceTest {
public static void main(String[] args) throws IOException {
String url = "http://ip:端口号/wsdl/IPacsServices";
String hisResult = "";
HashMap<String, Object> map = new HashMap<>();
map.put("OrderNo", "111111");
map.put("PatName", "test");
String soapAction = "http:PacsServicesIntf-IPacsServices/queryQueue";
SoapClient soapClient = SoapClient.create(url).setMethod("queryQueue", soapAction).
setParams(map);
soapClient.setReadTimeout(15000);//超时时间15秒。
soapClient.setConnectionTimeout(5000);//超时时间5秒。
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
hisResult = soapClient.send(true).replaceAll("<", "<").replaceAll(">", ">");
System.out.printf(hisResult);
}
}
二、使用hutool的HttpUtil.post方式
1)相关引用
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.11</version>
<scope>compile</scope>
</dependency>
2)实现代码
package com.lqt.test;
import cn.hutool.http.HttpUtil;
import java.io.IOException;
public class WebServiceTest {
public static void main(String[] args) throws IOException {
String url = "http://IP:端口/wsdl/IPacsServices";
String hisResult = "";
String paramPattern = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:PacsServicesIntf-IPacsServices\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <urn:queryQueue soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
" <OrderNo xsi:type=\"xsd:string\">111111</OrderNo>\n" +
" <PatName xsi:type=\"xsd:string\">test</PatName>\n" +
" </urn:queryQueue>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
hisResult = HttpUtil.post(url, paramPattern,12000).replaceAll("<", "<").replaceAll(">", ">");
System.out.printf(hisResult);
}
}
三、apache的HttpClient调用方式
1)相关引用
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
<scope>compile</scope>
</dependency>
2)实现代码
package com.lqt.test;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.dom4j.io.SAXReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class WebServiceTest {
public static void main(String[] args) throws IOException {
String url = "http://IP:端口号/soap/IPacsServices";
String hisResult = "";
String paramPattern = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:PacsServicesIntf-IPacsServices\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <urn:queryQueue soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
" <OrderNo xsi:type=\"xsd:string\">11111111</OrderNo>\n" +
" <PatName xsi:type=\"xsd:string\">test</PatName>\n" +
" </urn:queryQueue>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
HttpClient httpClient = new HttpClient();
PostMethod httppost = new PostMethod(url);
/* 把Soap请求数据添加到PostMethod */
try {
httppost.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
// 设置SOAPAction
httppost.setRequestHeader("SOAPAction", "http://tempuri.org/IPacsServices/queryQueue");
byte[] b = paramPattern.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length,"application/soap+xml; charset=utf-8");
httppost.setRequestEntity(re);
httpClient.executeMethod(httppost);
InputStream rs = httppost.getResponseBodyAsStream();
// 获取返回流并转换为Document
org.dom4j.Document document = new SAXReader().read(rs, "UTF-8");
String result = document.asXML();
hisResult = result.replaceAll("<", "<").replaceAll(">", ">");
} catch (Exception e) {
// log.info("getChouxueResponse", e);
} finally {
httppost.releaseConnection();
}
System.out.println("return:" + hisResult);
}
}
四、net的http调用方式
1)相关引用
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
<scope>compile</scope>
</dependency>
2)代码实现
package com.lqt.test;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class WebServiceTest {
public static void main(String[] args) {
String url = "http://IP:端口/soap/IPacsServices?wsdl";
String paramPattern = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:PacsServicesIntf-IPacsServices\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <urn:queryQueue soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
" <OrderNo xsi:type=\"xsd:string\">1111111</OrderNo>\n" +
" <PatName xsi:type=\"xsd:string\">test</PatName>\n" +
" </urn:queryQueue>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
try {
String host = "IP:端口";
String SOAPAction = "urn:PacsServicesIntf-IPacsServices#queryQueue";
//接受返回报文
String result = new String();
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoInput(true);
//允许对外输出数据
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Host", host);
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
//soap
conn.setRequestProperty("SOAPAction", SOAPAction);
conn.setRequestProperty("Content-Length", String.valueOf(paramPattern.length()));
conn.setRequestMethod("POST");
//定义输出流
OutputStream output = conn.getOutputStream();
if (StringUtils.isNotBlank(paramPattern)) {
byte[] b = paramPattern.getBytes("UTF-8");
//发送soap请求报文
output.write(b, 0, b.length);
output.flush();
output.close();
//定义输入流,获取soap报文
InputStream input = conn.getInputStream();
//设置编码格式
Document document = new SAXReader().read(input, "UTF-8");
result = document.asXML();
input.close();
}
System.out.println("请求返回报文:" + result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
PS:访问用的url,使用saopUI中的url
有些厂商提供的wsdl路径无法直接使用。例如:http://IP:端口/wsdl/IPacsServices?wsdl直接使用无法访问到对应的接口,而应当替换成soap中转换的url(http://IP:端口/soap/IPacsServices?wsdl)