首页 > 编程 > Java > 正文

java调用短信api接口发送短信demo实例

2019-11-06 08:45:13
字体:
来源:转载
供稿:网友

java调用短信api接口发送短信demo实例

import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.PostMethod;/** * 发送短信验证码 * 申请地址:http://sms.webchinese.cn  * 需要的jar  commons-httpclient-3.1.jar   commons-logging-x.x.x.jar   commons-codec-x.x.jar * @author Admin * */public class SendMsg {public static void main(String[] args) throws Exception {    String s ="961210";        HttpClient client = new HttpClient();          PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn/");          post.addRequestHeader("Content-Type",                  "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码          NameValuePair[] data = { new NameValuePair("Uid", "z1780675810"), // 注册的用户名                  new NameValuePair("Key", "5c52043e4c6deb16563d"), // 注册成功后,登录网站使用的密钥                  new NameValuePair("smsMob", "要发送的手机号"), // 手机号码                  new NameValuePair("smsText", "测试地址,欢迎使用本公司发送测试验证码为:"+698210+"请妥善保管") };//设置短信内容              post.setRequestBody(data);      client.executeMethod(post);      Header[] headers = post.getResponseHeaders();      int statusCode = post.getStatusCode();      System.out.PRintln("statusCode:" + statusCode);      for (Header h : headers) {          System.out.println(h.toString());      }      String result = new String(post.getResponseBodyAsString().getBytes(              "utf-8"));      System.out.println(result);      post.releaseConnection();  }}

注意签名一定要跟网站注册的时候一样 不然返回结果为-51  就是签名有问题 

签名内容一定要放在短信文本里面!!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表