首页 > 编程 > Java > 正文

JAVA实现第三方短信发送过程详解

2019-11-26 08:37:43
字体:
来源:转载
供稿:网友

想使代码生效需要注册:

http://sms.webchinese.cn/default.shtmlhttp://sms.webchinese.cn/default.shtml

在muven项目里面导入jar包

<dependencies>  <dependency>    <groupId>commons-codec</groupId>    <artifactId>commons-codec</artifactId>    <version>1.4</version>  </dependency>  <dependency>    <groupId>commons-httpclient</groupId>    <artifactId>commons-httpclient</artifactId>    <version>3.1</version>  </dependency>  <dependency>    <groupId>commons-logging</groupId>    <artifactId>commons-logging</artifactId>    <version>1.1.1</version>  </dependency>  <dependency>    <groupId>org.apache.httpcomponents</groupId>    <artifactId>httpclient</artifactId>    <version>4.3.1</version>  </dependency>  <dependency>    <groupId>com.alibaba</groupId>    <artifactId>druid</artifactId>    <version>1.0.8</version>  </dependency>  <dependency>    <groupId>com.alibaba</groupId>    <artifactId>druid</artifactId>    <version>1.0.8</version>  </dependency></dependencies>

创建一个测试类:下面代码粘贴上去用就行了

public static void main(String[] args) throws HttpException, IOException {  // TODO Auto-generated method stub  HttpClient client = new HttpClient();  PostMethod post = new PostMethod("http://gbk.api.smschinese.cn/");  post.addRequestHeader("Content-Type",      "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码  NameValuePair[] data = { new NameValuePair("Uid", "****"), // 注册的用户名      new NameValuePair("Key", "********"), // 这个key需要在里面获取      new NameValuePair("smsMob", "*********"), // 手机号码      new NameValuePair("smsText", "嘻嘻!猜猜我是谁?") };//设置短信内容  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(      "gbk"));  System.out.println(result);  post.releaseConnection();}

输出结果成功后是这个效果;这个值代表短信发送的数量如果下面的返回值不是1请参照以下图片

可能需要的的问题:

当然 返回值等于1的时候不代表你就能收到短信 只能说明你短信发送成功了想要短信成功的接收不需要进去官网去申请短信接收接口

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

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