Search code examples
delphiindyidhttp

Sending SMS through API from Delphi


I want to send SMS from Delphi using an online API.

The API is provided by a service provider that works when used through a web browser, as below:

http://sendpk.com/api/sms.php?username=xxxx&password=xxxx&sender=Masking&mobile=xxxx&message=Hello

The above url works fine when opened through a web browser, and the SMS is sent successfully. Now, I am struggling to integrate the API into my Delphi application.

By searching through the Internet, I have found some examples, and finally I tried the below code:

var
  lHTTP: TIdHTTP;
  lParamList: TStringList;
begin
  lParamList := TStringList.Create;
  lParamList.Add('username=xxxx');
  lParamList.Add('password=xxxx');
  lParamList.Add('sender=Masking');
  lParamList.Add('mobile=xxxx');
  lParamList.Add('message=Hello');

  lHTTP := TIdHTTP.Create;    

  try
    PostResult.Lines.Text := lHTTP.Post('http://sendpk.com/api/sms.php', lParamList);
  finally
    lHTTP.Free;
    lParamList.Free;
  end;

But I am getting an error:

HTTP/1.1 406 Not Acceptable

Error Screen shot

The API Reference, as provided on service provider's website, is available below:

http://sendpk.com/api.php

Kindly guide me. What am I doing wrong, and what is the right code to use?

Edit

C# code provided in the API reference is as below:

using System;
using System.Net;
using System.Web;

public class Program
{
    public static void Main()
    {
        string MyUsername = "userxxx"; //Your Username At Sendpk.com
        string MyPassword = "xxxx"; //Your Password At Sendpk.com
        string toNumber = "92xxxxxxxx"; //Recepient cell phone number with country code
        string Masking = "SMS Alert"; //Your Company Brand Name
        string MessageText = "SMS Sent using .Net";
        string jsonResponse = SendSMS(Masking, toNumber, MessageText, MyUsername, MyPassword);
        Console.Write(jsonResponse);
        //Console.Read(); //to keep console window open if trying in visual studio
    }

    public static string SendSMS(string Masking, string toNumber, string MessageText, string MyUsername , string MyPassword)
    {
        String URI = "http://sendpk.com" +
            "/api/sms.php?" +
            "username=" + MyUsername +
            "&password=" + MyPassword +
            "&sender=" + Masking +
            "&mobile=" + toNumber +
            "&message=" + Uri.UnescapeDataString(MessageText); // Visual Studio 10-15
        try
        {
            WebRequest req = WebRequest.Create(URI);
            WebResponse resp = req.GetResponse();
            var sr = new System.IO.StreamReader(resp.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }
        catch (WebException ex)
        {
            var httpWebResponse = ex.Response as HttpWebResponse;
            if (httpWebResponse != null)
            {
                switch (httpWebResponse.StatusCode)
                {
                    case HttpStatusCode.NotFound:
                        return "404:URL not found :" + URI;
                        break;
                    case HttpStatusCode.BadRequest:
                        return "400:Bad Request";
                        break;
                    default:
                        return httpWebResponse.StatusCode.ToString();
                }
            }
        }
        return null;
    }
}

Solution

  • You need to use TIdHTTP.Get() instead of TIdHTTP.Post():

    var
      lHTTP: TIdHTTP;
      lUser, lPass, lSender, lMobile, lMsg: string;
    begin
      lUser := 'xxxx';
      lPass := 'xxxx';
      lSender := 'Masking';
      lMobile := 'xxxx';
      lMsg := 'Hello';
    
      lHTTP := TIdHTTP.Create;
      try
        PostResult.Lines.Text := lHTTP.Get('http://sendpk.com/api/sms.php?username=' + TIdURI.ParamsEncode(lUser) + '&password=' + TIdURI.ParamsEncode(lPass) + '&sender=' + TIdURI.ParamsEncode(lSender) + '&mobile=' + TIdURI.ParamsEncode(lMobile) + '&message=' + TIdURI.ParamsEncode(lMsg));
      finally
        lHTTP.Free;
      end;
    end;
    

    Update: a 406 response code means the server could not return a response in a format that would be acceptable to the client based on the client's Accept... request header(s) (Accept, Accept-Language, Accept-Encoding, etc), so check your headers against what the API is expecting and what a browser sends.