Springboot調用WebService(asmx)服務接口
導入httpclient jar
<!-- webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
1. 利用HttpURLConnection通過soap調用接口
public static void test() {
String xml =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<soap:Envelope">\n"
+ " <soap:Body>\n"
+ " <GetLeave xmlns=\"http://tem.org/\">\n"
+ " <Username>aaa</Username>\n"
+ " </GetLeave>\n"
+ " </soap:Body>\n"
+ "</soap:Envelope>";
// 服務器地址
String urlString = "http://xxxxx.asmx";
// 需要調用的方法
String soapActionString = "GetLeaveBalance";
URL url;
try {
url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 拼接soap
String soap = xml;
byte[] buf = soap.getBytes("UTF-8");
// 設置報頭
conn.setRequestProperty("Content-Length", String.valueOf(buf.length));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("soapActionString", soapActionString);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream out = conn.getOutputStream();
out.write(buf);
out.close();
// 獲取響應狀態碼
int code = conn.getResponseCode();
StringBuffer sb = new StringBuffer();
if (code == HttpStatus.OK.value()) {
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = is.read(b)) != -1) {
String s = new String(b, 0, len, "utf-8");
sb.append(s);
}
is.close();
}
System.out.println(sb);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
2. 使用HttpClient
public static void test2() throws Exception {
// 輸入服務網址
HttpClient client = new HttpClient();
// HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));
// GetMethod
PostMethod post = new PostMethod("http:/xxxx.asmx/getMethod");
// 設置參數
post.setParameter("username", "aa");
post.setParameter("password", "bddd");
// client.setTimeout(newTimeoutInMilliseconds);
// 執行,返回一個結果碼
int code = client.executeMethod(post);
System.out.println("結果碼:" + code);
// 獲取xml結果
String result = post.getResponseBodyAsString();
System.out.println("結果:" + result);
// 釋放連接
post.releaseConnection();
// 關閉連接
((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
}
3JaxWsDynamicClientFactory
public static void test3() {
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
// wsdlUrl webservice地址,加上?wsdl后綴
Client client = clientFactory.createClient("http://172.18.120.21/MWSLeaveService/MWSLeaveService.asmx?wsdl");
// name_space 命名空間
QName qName = new QName("http://tempuri.org", "GetLeaveBalance");
try {
//參數數組
String Username = "igs";
String Password = "igshkws";
String StaffID = "00441";
String LeaveType = "Item01" ;
Object[] param = new Object[]{Username,Password,StaffID,LeaveType};
Object[] res = client.invoke(qName, param);
// 處理結果res[0]
JSONObject jsonObject = JSONObject.parseObject(String.valueOf(res[0]));
// List<String> aList = JSONObject.parseArray(jsonObject.get("data").toString(),String.class);
// 業務
} catch (Exception e) {
e.printStackTrace();
}
}