通过API从Delphi发送短信

时间:2018-06-30 11:57:57

标签: delphi indy idhttp

我想使用在线API从Delphi发送短信。

该API由服务提供商提供,该服务提供商通过网络浏览器使用时可以正常工作,如下所示:

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

通过网络浏览器打开上述网址时,它正常工作,并且SMS发送成功。现在,我正在努力将API集成到我的Delphi应用程序中。

通过Internet搜索,我找到了一些示例,最后我尝试了以下代码:

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;

但是我遇到一个错误:

HTTP/1.1 406 Not Acceptable

Error Screen shot

下面提供了服务提供商网站上提供的API参考:

http://sendpk.com/api.php

请引导我。我在做什么错,要使用什么正确的代码?

修改

API参考中提供的

C#代码如下:

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

1 个答案:

答案 0 :(得分:1)

您需要使用TIdHTTP.Get()而不是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;

更新406响应代码表示服务器无法基于客户端的Accept...请求标头以客户端可接受的格式返回响应( s)(AcceptAccept-LanguageAccept-Encoding等),因此请根据API期望的内容以及浏览器发送的内容检查标头。