一、WebService接口说明
以http post的形式进行发送,上面是请求信息,下面是返回值。
接口文档返回值部分介绍。
二、Java代码,其中的MD5工具类请参照我的其他文章,或自己找一个MD5加密工具类。最终的MD5结果为32为大写。
public class SendSMS { //短信接口地址 private static String Url = "http://sdk.entinfo.cn:8061/webservice.asmx/mdsmssend"; public static Integer send(String mobile) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(Url); client.getParams().setContentCharset("UTF-8"); method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8"); int mobile_code = (int)((Math.random()*9+1)*100000); String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。"); NameValuePair[] data = {//封装参数 new NameValuePair("sn", "你自己的sn"), //密码可以使用明文密码或使用32位MD5加密 new NameValuePair("pwd", MD5.md5("你自己的sn和pwd").toUpperCase()), new NameValuePair("mobile", mobile), new NameValuePair("content", content), new NameValuePair("ext", ""), new NameValuePair("stime", ""), new NameValuePair("rrid", ""), new NameValuePair("msgfmt", "") }; method.setRequestBody(data); try { client.executeMethod(method); //获取返回的Xml String SubmitResult =method.getResponseBodyAsString(); //解析Xml Document doc = DocumentHelper.parseText(SubmitResult); Element root = doc.getRootElement(); Long returnValue = Long.parseLong(root.getText()); //对照返回值信息 为负数就是出现错误,>0说明发送成功(具体看返回的消息进行解析) //返回生成的验证码与用户输入的进行验证 if(returnValue > 0){ return mobile_code; }else { return null; } } catch (HttpException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } catch (DocumentException e) { e.printStackTrace(); return null; } catch (NumberFormatException e) { e.printStackTrace(); return null; } } }
三、算了我还是把MD5工具类贴一个出来吧。
import java.security.MessageDigest; public class MD5 { public static String md5(String str) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } str = buf.toString(); } catch (Exception e) { e.printStackTrace(); } return str; } public static void main(String[] args) { System.out.println(md5("sn"+"pwd")); } }