前端之家收集整理的这篇文章主要介绍了
手动发送xml报文调用webservice,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public String sendMessageByWebService2(String phone,String msg,String... args){
String webserviceUrl = PropertiesHandle.readValue("SMS.webserviceurl");
String userName = PropertiesHandle
.readValue("SMS.webserviceusername");
String mainCode = PropertiesHandle
.readValue("SMS.webservicemaincode");
String pwd = PropertiesHandle.readValue("SMS.webservicepwd");
String RmTfh = PropertiesHandle.readValue("SMS.webservicermtfh");
String result = "";
URL url = null;
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb
.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
sb.append("<soap:Body>");
sb
.append("<ns1:getBalance xmlns:ns1=\"http://webService.sooyie.com/\">");
sb.append("<arg0>"+mainCode+"</arg0>");
sb.append("<arg1>"+pwd+"</arg1>");
sb.append("</ns1:getBalance>");
sb.append("</soap:Body></soap:Envelope>");
try {
url = new URL(webserviceUrl);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
URLConnection uc = null;
OutputStreamWriter out = null;
BufferedReader rd = null;
try {
uc = url.openConnection();
uc.setDoOutput(true); // ....必须设置为'true'.
uc.setRequestProperty("Content-Type","text/xml"); // 记住这行不能少否则会出错
out = new OutputStreamWriter(uc.getOutputStream(),"utf-8");
out.write(sb.toString());
out.flush();
out.close();
rd = new BufferedReader(new InputStreamReader(uc.getInputStream(),"utf-8"));
StringBuffer responseStr = new StringBuffer();
String str;
while ((str = rd.readLine()) != null) {
responseStr.append(str);
}
result = responseStr.substring(responseStr.indexOf("<return>") + 8,responseStr.indexOf("</return>"));
System.out.println(result);
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
原文链接:https://www.f2er.com/xml/299441.html