axis调用webservice接口

情到浓时终转凉″ 提交于 2021-02-15 04:02:24
老版本1.6之前的jdk对CFX调用webservice支持很差,故本文采用axis的调用方式。





代码示例:

package com.cssun.util;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import java.io.UnsupportedEncodingException;
import java.rmi.RemoteException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SendMsgClient {
private String url;

private String smsStyle;//身份验证用户名

private String smsSecret;//身份验证密钥


public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getSmsStyle() {
return smsStyle;
}

public void setSmsStyle(String smsStyle) {
this.smsStyle = smsStyle;
}

public String getSmsSecret() {
return smsSecret;
}

public void setSmsSecret(String smsSecret) {
this.smsSecret = smsSecret;
}

/**
* 转换XML格式
* @param mobile 手机号
* @param message 发送内容
* @return
*/
public String getContent(String mobile,String message){
String formatString="<?xml version=\"1.0\" encoding=\"GBK\"?><sms><SmsStyle>%s</SmsStyle><item><MobileNo>%s</MobileNo><SendContent>%s</SendContent><SendTime></SendTime><Signature>%s</Signature></item></sms>";
return String.format(formatString,getSmsStyle(),mobile,message,getMd5Code(String.format("%s%s%s%s",getSmsStyle(),mobile,message,smsSecret)));//转换XML格式
}

/**
* MD5加密
* @param strObj
* @return
*/
public String getMd5Code(String strObj) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(strObj.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();

for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}

/**
* axis调用webservice接口发送短信
* @param mobile 手机号
* @param content 发送内容
*/
public boolean sendSms(String mobile,String content) {
boolean flag = false;
try {
// 服务端的url,需要根据情况更改。
String endpointURL = "http://sms.itl.cn/services/SendSms?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpointURL));
call.setOperationName(new QName("http://action.sendMessage.yeeku.org","sendMessage"));// 设置操作的名称。
/*// 由于需要认证,故需要设置调用的用户名和密码。 此处参数根据具体接口需求添加
SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("http://tempuri.org/", "AuthHeaderCS");
soapHeaderElement.setNamespaceURI("http://tempuri.org/");
try
{
soapHeaderElement.addChildElement("Username").setValue("admin");
soapHeaderElement.addChildElement("Password").setValue("123");
}
catch (SOAPException e)
{
e.printStackTrace();
}
call.addHeader(soapHeaderElement);*/
call.setReturnType(XMLType.XSD_STRING);// 返回的数据类型
call.addParameter("requeString", XMLType.XSD_STRING, ParameterMode.IN);// 参数的类型
String result = (String) call.invoke(new Object[] { getContent(mobile,content) });// 执行调用
//解析返回值
Document document = null;
Element root = null;
document = DocumentHelper.parseText(result);
root = document.getRootElement();
String resultFlag = root.elementText("ResultFlag");
if("1".equals(resultFlag)){
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}

public static void main(String[] args) throws ServiceException, RemoteException {
SendMsgClient a = new SendMsgClient();
a.setSmsStyle("157");//权限验证 key
a.setSmsSecret("7788");//密钥
boolean t = a.sendSms("18511909417","哈哈哈");
System.out.println(t);
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!