delphi - 从 Delphi 通过 API 发送 SMS
问题描述
我想使用在线 API 从 Delphi 发送 SMS。
该 API 由服务提供商提供,可在通过 Web 浏览器使用时工作,如下所示:
http://sendpk.com/api/sms.php?username=xxxx&password=xxxx&sender=Masking&mobile=xxxx&message=Hello
通过网络浏览器打开上述网址时可以正常工作,并且短信发送成功。现在,我正在努力将 API 集成到我的 Delphi 应用程序中。
通过网上搜索,我找到了一些例子,最后我尝试了下面的代码:
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
服务提供商网站上提供的 API 参考如下:
请指导我。我做错了什么,正确的代码是什么?
编辑
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;
}
}
解决方案
您需要使用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...
请求标头(,,,Accept
等Accept-Language
)以客户端可接受的格式返回响应Accept-Encoding
,因此请检查您的标头是否符合 API 的预期和浏览器发送的内容。
推荐阅读
- python - Django 和纯 Python
- javascript - 如何按照给定的数组重新排列带有js编写的子元素的div框?
- r - 利用帽子值计数与自己计数
- c - 无法在openGL中绘制对象
- c++ - 有没有办法使用winapi打开蓝牙?
- javascript - 显示警报时如何自动滚动到顶部
- html - 将引导手风琴上的加减号图标移动到最右边
- postgresql - 为什么使用 JDBC 时 current_time 与 PostgreSQL 12 中的 current_timestamp 和 now() 都不同?
- python - 为什么两个循环方法第一次返回正确的结果,而第三个方法需要运行三次才能得到正确的结果?
- wordpress - 分配自定义类别以阻止 - Gutenberg - WordPress