使用 Kannel 发送简单的短信

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

  • 新项目:《从零手撸:仿小红书(微服务架构)》 正在持续爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17...点击查看项目介绍 ;
  • 《从零手撸:前后端分离博客项目(全栈开发)》 2 期已完结,演示链接: http://116.62.199.48/ ;

截止目前, 星球 内专栏累计输出 63w+ 字,讲解图 2808+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 2200+ 小伙伴加入学习 ,欢迎点击围观

通过 HTTP GET 发送短信


 Sending SMS via HTTP GET

package com.simpleget;

import java.io.IOException; import java.net.URI;

import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils;

public class simpleGet {

public static void main(String[] args) { //Create Http client

CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = null; CloseableHttpResponse response = null; HttpEntity entity = null;

try{ //Create GET URL URI uri = new URIBuilder() .setScheme("http") .setHost("localhost:13003") .setPath("/cgi-bin/sendsms") .setParameter("username", "abc") // kannel username .setParameter("password", "xyz") // kannel password .setParameter("smsc", "send") // smsc name .setParameter("dlr-mask", "31") .setParameter("from", "Test") // sender of message (can be any string or number) .setParameter("to", "980010.....") // destination (must be a mobile number) .setParameter("text", "hello sending test message") // message to send .build();

httpGet = new HttpGet(uri);

LOG.info(httpGet.getURI()); httpGet.setHeader("content-type", "text/plain");

response = httpclient.execute(httpGet);

    LOG.info(response.getStatusLine());
entity = response.getEntity();

EntityUtils.consume(entity);

} catch (Exception e) {

e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) {

e.printStackTrace(); } }

}

}

Sending SMS via HTTP POST

package com.simplepost;

import java.io.IOException; import java.io.InputStream;

import org.apache.commons.io.IOUtils; import org.apache.http.Consts; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils;

public class SimplePost {

public static void main(String[] args) { String url = "http://localhost:13013/cgi-bin/sendsms";

CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); CloseableHttpResponse response = null;

StringEntity entity = null; //For sending SMS using POST method requires whole message to be in xml string format. It has all the parameters that are in get only in xml format.

      String inputXML= "<message><submit><da><number>9780.....</number></da><oa><number>Test Sender</number></oa><ud>Hello +esting</ud><from><username>abc</username><password>xyz</password></from><to>send</to></submit></message>";

// While using HTTP POST you have to set content type to "text/xml" or else it wont work InputStream in ; entity = new StringEntity(inputXML, ContentType.create("text/xml", Consts.UTF_8)); entity.setChunked(true); try {

             httpPost = new HttpPost(url);

             httpPost.setEntity(entity);
             response = httpClient.execute(httpPost);

             System.out.println(response.getStatusLine());
             in=response.getEntity().getContent();
             String body = IOUtils.toString(in);
             System.out.println(body);
                EntityUtils.consume(entity);

} catch (Exception e) {

e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) {

e.printStackTrace(); } }

} }


Kannel 是一个开源的 SMS 网关,广泛用于发送 SMS(单个或批量 SMS)。

您可以在用户指南中获得有关如何配置和使用 kannel 的大部分信息

上面提供的代码片段具有使用 Kannel 通过 HTTP POST 和 HTTP GET 发送 SMS 的简单方法。

相关文章