方法一:
//url为wsdl路径
public static synchronized boolean sendMsgWebservice(String url,String content,String addresseeTel,String userAccount,String password,String appCode ) {
String sendTime=DateUtil.dateFormat(new Date(),"yyyy-MM-dd HH:mm:ss").replace(" ","T");
// 定义httpClient的实例
CloseableHttpClient httpclient = HttpClients.createDefault();
boolean flag=false;
CloseableHttpResponse response2=null;
try{
HttpPost httpPost = new HttpPost(url);
String wsdlData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
"<soap12:Body>\n" +
"<SendSms xmlns=\"http://tempuri.org/\">\n" +
"<content>"+content+"</content>\n" +
"<addresseeTel>"+addresseeTel+"</addresseeTel>\n" +
"<sendTime>"+sendTime+"</sendTime>\n" +
"<userAccount>"+userAccount+"</userAccount>\n" +
"<password>"+password+"</password>\n" +
"<appCode>"+appCode+"</appCode>\n" +
"</SendSms>\n" +
"</soap12:Body>\n" +
"</soap12:Envelope>";
StringEntity myEntity = new StringEntity(wsdlData,ContentType.create("text/xml", "UTF-8"));
httpPost.setEntity(myEntity);
response2 = httpclient.execute(httpPost);
HttpEntity entity2 = response2.getEntity();
String retVal=null;
if (entity2 != null) {
retVal = EntityUtils.toString(entity2, "UTF-8");
}
EntityUtils.consume(entity2);
if(retVal!=null && retVal.contains("发送成功")){
flag=true;
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(response2!=null){
response2.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
wsdl 数据格式
POST /SmpWebService/SmpWebService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://beyondbit.com/smp/SendSms"
//参数定义如下:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SendSms xmlns="http://beyondbit.com/smp">
<content>string</content>
<addresseeTel>string</addresseeTel>
<sendTime>dateTime</sendTime>
<userAccount>string</userAccount>
<password>string</password>
<appCode>string</appCode>
</SendSms>
</soap12:Body>
</soap12:Envelope>
//执行结果定义:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SendSmsResponse xmlns="http://beyondbit.com/smp">
<SendSmsResult>string</SendSmsResult>
</SendSmsResponse>
</soap12:Body>
</soap12:Envelope>
本文介绍了一种通过Webservice接口发送短信的方法,详细展示了如何使用Java实现与Webservice的交互,包括请求构造、响应处理及异常捕获。文章提供了完整的代码示例,帮助读者理解如何构建SOAP消息并调用远程服务。
1051

被折叠的 条评论
为什么被折叠?



