个人项目添加短信提醒功能基于jsp+servlet

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

平台:互亿无线 链接:https://user.ihuyi.com/new/console


提示:以下是本篇文章正文内容,下面案例可供参考

二、使用步骤

1.代码

代码如下(示例):

package servlet;//接口类型:互亿无线触发短信接口,支持发送验证码短信、订单通知短信等。
// 账户注册:请通过该地址开通账户http://sms.ihuyi.com/register.html
// 注意事项:
//(1)调试期间,请用默认的模板进行测试,默认模板详见接口文档;
//(2)请使用APIID(查看APIID请登录用户中心->验证码短信->产品总览->APIID)及 APIkey来调用接口;
//(3)该代码仅供接入互亿无线短信接口参考使用,客户可根据实际需要自行编写;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//import util.StringUtil;
@WebServlet("/sendsmsutf8Servlet")
public class sendsmsutf8Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        try {
            sendMsg();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        resp.getWriter().write("true");
        resp.flushBuffer();
    }



    private void sendMsg() throws IOException, DocumentException {
        String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
        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 + "。请不要把验证码泄露给其他人。");
        //System.out.println(content);
        NameValuePair[] data = {//提交短信
                new NameValuePair("account", "C03228779"), //查看用户名是登录用户中心->验证码短信->产品总览->APIID
                new NameValuePair("password", "895b26d8896f506bf6e8f0e2c0b3ebb9"),  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
                //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
                new NameValuePair("mobile", "18569631716"),
                new NameValuePair("content", content),
        };
        method.setRequestBody(data);


            client.executeMethod(method);

            String SubmitResult =method.getResponseBodyAsString();

            //System.out.println(SubmitResult);

            Document doc = DocumentHelper.parseText(SubmitResult);
            Element root = doc.getRootElement();

            String code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");

            System.out.println(code);
            System.out.println(msg);
            System.out.println(smsid);

            if("2".equals(code)){
                System.out.println("短信提交成功");
            }
    }

}

2.导入jar包

代码如下(示例):
在这里插入图片描述


总结

我前面遇到的主要问题就是请添加图片描述
因为我没有用官方文档提供的jar包,导致自己找的jar包一直存在问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值