public static String sendWebService(String content, String method){
// 这里要打一个LOG
int timeout = 60000;
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">");
sb.append("<soapenv:Header/>");
sb.append("<soapenv:Body>");
sb.append("<tem:").append(method).append(">");
sb.append("<tem:xml><![CDATA[").append(content).append("]]></tem:xml>");
sb.append("</tem:").append(method).append(">");
sb.append("</soapenv:Body>");
sb.append("</soapenv:Envelope>");
final String resultStrTag = method + "Result";
// HttpURLConnection 发送SOAP请求
try {
String urlStr = StaticProperties.HisUrl;
LogUtil.info("hisUrl:" + urlStr);
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
conn.setRequestProperty("SOAPAction", "http://tempuri.org/" + method);//Soap
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(sb.toString().getBytes(StandardCharsets.UTF_8));
dos.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String line;
StringBuilder strBuf = new StringBuilder();
while ((line = reader.readLine()) != null) {
strBuf.append(line);
}
dos.close();
reader.close();
String partStr = strBuf.toString();
partStr = partStr.replaceAll(">", ">").replaceAll("<", "<");
String resultStr = partStr.substring(partStr.indexOf("<" + resultStrTag + ">") + resultStrTag.length() + 2, partStr.indexOf("</" + resultStrTag + ">"));
// 这里要打一个LOG
return resultStr;
} catch (Exception e) {
e.printStackTrace();
// 这里要打一个LOG
}
return null;
}
java-webservice-SOAP调用方式
最新推荐文章于 2024-02-02 11:29:05 发布