提交了辞职信,工作交接中,闲来无事访问apache的网站,想起来曾一时让我很头疼的webservice,随手写了几行代码,就当练手吧!接下来学习axis2,奔着那个方向去。
这段代码是我在调用电信的服务时的客户端代码,实现下发手机短信的部分代码。重点1.stub方式ws的应用。2.怎么设置soap头。还是那句老话,从知道到不知道是一个很艰苦的过程。
这段代码是我在调用电信的服务时的客户端代码,实现下发手机短信的部分代码。重点1.stub方式ws的应用。2.怎么设置soap头。还是那句老话,从知道到不知道是一个很艰苦的过程。
public static void main(String[] args) throws InterruptedException
{
Test test = new Test();
try
{
//URL 调用webservice地址
SendSmsBindingStub service = (SendSmsBindingStub) new SendSmsServiceLocator().getSendSms(new URL("http://1.1.1.1:8080/SendSmsService"));
SOAPHeaderElement header = new SOAPHeaderElement("http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1","RequestSOAPHeader");
SOAPElement soap = null;
soap = header.addChildElement("password");
soap.addTextNode(test.getPassword().toUpperCase());
soap = header.addChildElement("productId");
Node node = doc.selectSingleNode("//cctc/projects/productid[@busiid='"+ busiid +"']");
if(node != null)
soap.addTextNode(node.getText());
else
soap.addTextNode("");
//....................
//设置头
service.setHeader(header);
//因设置不群发,所以去URL的第一个元素
String result = service.sendSms(new URI[0], "senderName", new ChargingInformation(), "message", new SimpleReference());
System.out.println(result);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private String getPassword()
{
//密码为接入网关号+接入密码+10为时间戳后MD5加密
String password = "xxxxxxx" + "yyyyyy" + getTimetemp();
MessageDigest md5 = getMD5();
md5.update(password.getBytes());
byte[] result = md5.digest();
StringBuffer strBuffer = new StringBuffer();
for(int i = 0; i < result.length; i++)
{
String temp = Integer.toHexString(result[i] & 0xff);
if(temp.length() == 1)
strBuffer.append("0");
strBuffer.append(temp);
}
return strBuffer.toString();
}
private SimpleDateFormat sdf = null;
private String getTimetemp()
{
if(sdf == null)
sdf = new SimpleDateFormat("MMddHHmmss");
return sdf.format(new Date(System.currentTimeMillis()));
}
private static MessageDigest getMD5()
{
MessageDigest md5 = null;
try
{
if(md5 == null)
md5 = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return md5;
}