首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用site2SMS/way2sms在java中发送SMS

使用site2SMS/way2sms在java中发送SMS
EN

Stack Overflow用户
提问于 2013-05-02 11:09:05
回答 1查看 10.2K关注 0票数 0

下面的代码是用JAVA语言发送sms,

代码语言:javascript
复制
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);
    }
} 

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

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

感谢并致以问候

EN

回答 1

Stack Overflow用户

发布于 2013-05-02 11:32:34

它是用您类中的注释编写的:替换您的site2sms用户名和密码。我想你得为smsapi.cikly.in的一个账户做些修改

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16336524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档