0

I am writing a Java Program which must be able to send SMS. I'm inspired by this post Here is my code

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);
        }

When I execute the answer is printed but the sms not sent, don't understand why. Thanks Here is what I get in the console

<!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>
Community
  • 1
  • 1
megueloby
  • 71
  • 1
  • 3
  • 11
  • Paste your message and response from server. You have to encode query parameter `msg` in case it contains whitespaces etc... – staszek Nov 10 '16 at 09:21
  • Possible duplicate of [Using java.net.URLConnection to fire and handle HTTP requests](http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests) – AxelH Nov 10 '16 at 09:24
  • Did you tried to send the SMS without Java using this URL ? Is it valid ? what is the response in that case ? – AxelH Nov 10 '16 at 09:25
  • Yes @AxelH on the browser the url work fine. – megueloby Nov 10 '16 at 09:27
  • You have an accounts for this API I suppose, is there a way to see some history, see if they receive your SMS request. Is the answer is the one you are awaiting ? – AxelH Nov 10 '16 at 09:34
  • Thanks @AxelH I redited the question – megueloby Nov 10 '16 at 10:39
  • Well, you are receiving an web page with a link ... there is not much information on their site so ... have you any documentation to provide about this API ? Where is this URL come from ? Are you sure you are correctly formating your URL ? There is some limitation about some character like `space` – AxelH Nov 10 '16 at 11:02

0 Answers0