对于外部系统发布的webservice接口WSDL。可使用HTTP请求方式去访问。
/**
* http post请求
* @param urlString 请求地址
* @param soapXML xml字符串
* @return
*/
public static String HttpPostRequest(String urlString , String soapXML){
String result = "";
HttpURLConnection connection = null ;
try{
//1:创建服务地址
URL url = new URL(urlString);
//2:打开到服务地址的一个连接
connection = (HttpURLConnection) url.openConnection();
//3:设置连接参数
//3.1设置发送方式:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:Content-type
connection.setRequestProperty("content-type", "text/xml;charset=UTF-8");
//3.3设置输入输出,新创建的connection默认是没有读写权限的,
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();//打开到此 URL 引用的资源的通信链接
//4:组织SOAP协议数据,发送给服务端
LOGGER.debug( "调接口xml为:"+soapXML);
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes("UTF-8"));//使用编码UTF-8
os.flush();//刷新输出缓冲区
//5:接收服务端的响应
int responseCode = connection.getResponseCode();
LOGGER.debug("responseCode:" + responseCode);
StringBuilder sb = new StringBuilder();
if(responseCode == 200){//表示服务端响应成功
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
LOGGER.debug("接口返回的xml为:"+sb.toString());
result = sb.toString();
is.close();
isr.close();
br.close();
}else{
result = "请求返回码为:"+responseCode+"";
}
os.close();
connection.disconnect();
}catch (Exception ex){
ex.printStackTrace();
eservice.saveException(ex);
} finally {
if (connection != null) {
connection.disconnect();
}
}
return result ;
}