关于短信发送与HTTP请求的那些事

来源:互联网 发布:java编写验证身份证号 编辑:程序博客网 时间:2024/06/11 03:03


我一直就纳闷短信发送这个东西是怎么做的,之前驻场的地方说没测试环境的短信发送,让直接上生产,心里有千万个草泥马登山包飞翔而过,然而后来的需求改了不需要了,单还是飞翔着登山包。如今主要进行版本迭代,看到有个短信发送写好的东西,想到备用故写此文,结果发现水还挺深的。


先贴代码

public static boolean sendSmsActivateFriends(String mobile, String content, String type) {//短信发送开关if(UN_SMS_SWITCH.equals("1")){HttpClient client = new HttpClient();PostMethod method = new PostMethod(SMSURL);client.getParams().setContentCharset("UTF-8");method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");NameValuePair[] data = {//提交短信参数new NameValuePair("account",SMS_ACCOUNT), new NameValuePair("password",SMSPASSWORD),new NameValuePair("mobile", mobile), new NameValuePair("content", content),};method.setRequestBody(data);try {client.executeMethod(method);String SubmitResult =method.getResponseBodyAsString();Document doc = DocumentHelper.parseText(SubmitResult);Element root = doc.getRootElement();String code = root.elementText("code");//返回状态码枚举String msg = root.elementText("msg");//提交结果的描述//String smsid = root.elementText("smsid");//此消息的id,在发送成功的时候返回LOGGER.info("互易无线短信发送状态【{}】,手机号为【{}】,结果【{}】", new Object[]{code,mobile,msg});if("2".equals(code)){return true;}} catch (Exception e) {LOGGER.error("Exception:【{}】", e);LOGGER.error(e.getLocalizedMessage());}return false;}else{return true;}}

反正大概能看明白怎么个意思,就是对于HTTP的东西不了解,故而在网上找到了一哥们儿写的文,挺通俗易懂的,请自行打开链接阅读。

http://blog.csdn.net/chenyi_home/article/details/17240457


可能对接不同的短信第三方方法参数不太一样,原理上应该差不多。贴出来备用,万一能用上呢?