1、调用第三方提供的webservice接口
1、获取报文信息
SoapUI-x64-5.2.1获取链接:https://pan.baidu.com/s/1iDKyUQXC4qjsKjHzdq3QcQ
提取码:s91m
2、调用
1、调用HTTP接口
public static String invokeService(String serviceURL,String soapXML) {
String str = "";
try {
//第一步:创建服务地址
URL url = new URL(serviceURL);
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数 //3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
connection.setRequestProperty("SOAPAction","text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes("utf-8"));
os.close();
int responseCode = connection.getResponseCode();
if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) {
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}
br.close();
str = sb.toString();
}else {
InputStream is = connection.getErrorStream();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
br.close();
str = sb.toString();
}
} catch (IOException e) {
logger.warn(e.getMessage());
}
return str;
}
2、调用HTTPS接口
如果服务端设置了证书校验,调用https接口就会出现证书问题,请求端可以设置跳过证书校验。
public static String invokeHttpsService(String serviceURL,String soapXML) {
String str = "";
try {
SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
TrustManager[] tm = {
new MyX509TrustManager()};
try {
sslcontext.init(null, tm, new SecureRandom(