java - 使用site2SMS/way2sms在java中发送短信

标签 java smslib

以下代码是用JAVA语言发送短信,

import java.net.*;
import java.io.*;
import java.util.Scanner;

public class site2sms
{
    //Replace your site2sms username and password below
    static final String _userName = "username";
    static final String _password = "password";
    static final String _url = "http://smsapi.cikly.in/index.php";  
    //static final String _url = "http://smsapi.cikly.in/index.php";
    //static final String _url = "http://www.site2sms.com/user/send_sms_next.asp";
    static final String charset = "UTF-8";

    //to build the query string that will send a message
    private static String buildRequestString(String targetPhoneNo, String message) throws UnsupportedEncodingException
    {
        String [] params = new String [5];
        params[0] = _userName;
        params[1] = _password;
        params[2] = message;
        params[3] = targetPhoneNo;
        params[4] = "site2sms";

        String query = String.format("uid=%s&pwd=%s&msg=%s&phone=%s&provider=%s",
        URLEncoder.encode(params[0],charset),
        URLEncoder.encode(params[1],charset),
        URLEncoder.encode(params[2],charset),
        URLEncoder.encode(params[3],charset),
        URLEncoder.encode(params[4],charset)
        );
    return query;
    }

    public static void sendMessage(String reciever, String message) throws Exception
    {

        System.out.println("hi!hello");
        //To establish the connection and perform the post request
        URLConnection connection = new URL(_url + "?" + buildRequestString(reciever,message)).openConnection();
        connection.setRequestProperty("Accept-Charset", charset);

        //This automatically fires the request and we can use it to determine the response status
        InputStream response = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(response));
        //System.out.println(br);

        System.out.println(br.readLine());
    }

    public static void main(String [] args) throws Exception
    {
        System.out.println("enter Mobile No:");
        Scanner scanIn = new Scanner(System.in);

        String testPhoneNo = scanIn.nextLine();
        scanIn.close();            
        String testMessage = "Sending Messages From java is not too hard";
        sendMessage(testPhoneNo,testMessage);
    }
} 

当我运行程序时,没有编译错误,也没有运行时错误,但是消息没有传递到相应的号码。

请帮助我,任何建议都可以接受。

感谢和问候,

最佳答案

您类(class)的评论中写着:替换您的 site2sms 用户名和密码。我猜你必须在 smsapi.cikly.in 订阅一个帐户

关于java - 使用site2SMS/way2sms在java中发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16336524/

相关文章:

java - 如何使用 SMSlib 更改从 gsm 调制解调器向我的应用程序通知新消息的时间段

java - 使用 SMSLib 和 Web 服务发送 SMS

java - 无法使用java发送短信

java - For循环在Hibernate中保存数据而不添加所有项目

用于日志记录的 Java 对象序列化

c# - 如何在 C# 中执行类似 Java 或 Qt 的多线程编程

java - 如何配置 Log4j 以读取我的属性文件?

java - 如何在 SMSLib 中分配发件人姓名?

java - 如何因 Firebase 存储中的文件大小限制规则而出现拒绝上传错误

java - Apache 德鲁伊 没有已知的服务器