public static String ncgConnection(String wsdUrl, String params) {
URL wsUrl;
String result="";
try {
wsUrl = new URL(wsdUrl);
HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“Content-Type”, “text/xml;charset=UTF-8”);
conn.setRequestProperty(“SOAPAction”,“urn:SpdMessage”);
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
OutputStream os = conn.getOutputStream();
String soap = “<soapenv:Envelope xmlns:soapenv=“http://schemas.xmlsoap.org/soap/envelope/” xmlns:urn=“urn:hl7-org:v3”>” +
“soapenv:Header/” +
“soapenv:Body” +
“” +
“<apply_data>”+
params+
“</apply_data>”+
“” +
“</soapenv:Body>” +
“</soapenv:Envelope>”;
System.out.println(“包装后的参数为:”+params);
os.write(soap.getBytes());
int code = conn.getResponseCode();
System.out.println(“返回的code为:”+code);
InputStream is;
if (code == 200) {
is = conn.getInputStream();
} else {
is = conn.getErrorStream();
}
byte[] b = new byte[2048];
int len = 0;
String s = “”;
while((len = is.read(b)) != -1){
String ss = new String(b,0,len,“UTF-8”);
System.out.println(“收到的ss为:”+ss);
s += ss;
}
System.out.println(“收到的s为:”+s);
result=s.split("<apply_data>")[1].split("</apply_data>")[0];
System.out.println(“得到的返回值为:”+result);
is.close();
os.close();
conn.disconnect();
} catch (MalformedURLException e) {
System.out.println(“通讯模块1:”+e.getMessage());
} catch (IOException e) {
System.out.println(“通讯模块2:”+e.getMessage());!
}
return result;
}
在请求头中设置Content-Type,SOAPAction这两个设置错误会报错。The endpoint reference (EPR) for the Operation not found…
可以使用soapui访问后会得到如图。根据如图去添加请求头的信息。
消息内容的封装:
根据soapUI的访问内容封装。
soap1.2也根据soapui去封装。