从java应用程序通过java发送短信

时间:2016-11-10 09:20:07

标签: java sms urlconnection

我正在编写一个必须能够发送短信的Java程序。我受到post的启发 这是我的代码

String url = "http://www.mobinawa.com/http_api";
        String charset = "UTF-8";  // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name()
        String param1 = "sendsms";
        String param2 = "696926448";
        String param3 = "pwd";
        String param4 = "HEREKA";
        String param5 = phoneNumber.replace("+237", "");
        String param6 = textMessage;

        String query = String.format("action=%s&username=%s&password=%s&from=%s&to=%s&msg=%s", 
        URLEncoder.encode(param1, charset), 
        URLEncoder.encode(param2, charset),
        URLEncoder.encode(param3, charset), 
        URLEncoder.encode(param4, charset),
        URLEncoder.encode(param5, charset), 
        URLEncoder.encode(param6, charset));




        /*
        URLConnection connection = new URL(url + "?" + query).openConnection();
        connection.setRequestProperty("Accept-Charset", charset);*/


        URLConnection connection = new URL(url).openConnection();
        connection.setDoOutput(true); // Triggers POST.
        connection.setRequestProperty("Accept-Charset", charset);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
        try (OutputStream output = connection.getOutputStream()) {
            output.write(query.getBytes(charset));
        }



        InputStream response = connection.getInputStream();
        try (Scanner scanner = new Scanner(response)) {
            String responseBody = scanner.useDelimiter("\\A").next();
            System.out.println(responseBody);
        }

当我执行答案时打印但短信没有发送,不明白为什么。 谢谢 这是我在控制台中得到的内容

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
    <head>
        <title></title>
        <meta name="description" content="">
        <meta name="keywords" content="">
        <meta name="generator" content="ORT - Ovh Redirect Technology">
        <meta name="url" content="http://digitalmarketing.gts-africa.com/http_api">
        <meta name="robots" content="all">
    </head>
    <frameset rows="100%,0" frameborder=no border=0>
        <frame name="ORT" src="http://digitalmarketing.gts-africa.com/http_api">
        <frame name="NONE" src="" scrolling="no" noresize>
        <noframes>
            <body><a href="http://digitalmarketing.gts-africa.com/http_api">Click here</a><hr></body>
        </noframes>
    </frameset>
</html>

0 个答案:

没有答案