一般情况下,我们可以通过soapUi请求验证是否成功
对于webService的http请求,请求头必须添加SOAPAction
connection.setRequestProperty(“SOAPAction”,“application/soap+xml; charset=utf-8”);
xml转json需要用到hutool-all-4.0.12.jar,解析json比xml要简单得多。
如果不需要用xml转json,除需要jdk自带的包不再需要引入其他的包。
package com.baidu.util;
import cn.hutool.json.XML;
import net.sf.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @version 1.0.0
* @ClassName SoapUtil.java
* @Description webservice调用工具类
* @createTime 2020/9/29 10:37
*/
public class SoapUtil {
/**
* 发送http post调用webservice
*
* @param strUrl
* @param body
* @return
* @throws IOException
*/
public static String soapPost(String strUrl, String body) throws IOException {
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
StringBuilder responseResult = new StringBuilder();
HttpURLConnection httpURLConnection = null;
//第一步:创建服务地址,不是WSDL地址
try {
URL url = new URL(strUrl);
//第二步:打开一个通向服务地址的连接
httpURLConnection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
// 设置通用的请求属性
httpURLConnection.setConnectTimeout(20 * 1000);
httpURLConnection.setReadTimeout(30 * 1000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//请求头必须设置SOAPAction
httpURLConnection.setRequestProperty("SOAPAction", "application/soap+xml; charset=utf-8");
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
printWriter = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8"));
printWriter.write(body);
// flush输出流的缓冲
printWriter.flush();
//第五步:接收服务端响应,打印
int responseCode = httpURLConnection.getResponseCode();
StringBuilder sb = new StringBuilder();
if (200 == responseCode) {//表示服务端响应成功
// 定义BufferedReader输入流来读取URL的ResponseData
bufferedReader = new BufferedReader(new InputStreamReader(
httpURLConnection.getInputStream(), Charset.forName("UTF-8")));
String line;
while ((line = bufferedReader.readLine()) != null) {
responseResult.append(line);
}
} else {
throw new RuntimeException("调用webservice失败:服务器端返回HTTP code " + responseCode + "信息:");
}
return sb.toString();
} catch (Exception e) {
} finally {
try {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
if (printWriter != null) {
printWriter.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
}
}
public static void main(String[] args) {
String url = "http://localhost:9091/demo/validate";
String body = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.webservice.caau.demo.com/\">\n"
+ " <soapenv:Header/>\n"
+ " <soapenv:Body>\n"
+ " <ser:validate>\n"
+ " <certBase64>123456</certBase64>\n"
+ " </ser:validate>\n"
+ " </soapenv:Body>\n"
+ "</soapenv:Envelope>";
System.out.println(soapPost(url,body));
}
}