Delphi短信接口、Delphi短信验证码接口源码、Delp

时间:2016-06-13 10:39 来源:原创 作者:admin

Delphi短信接口、Delphi短信验证码接口源码、Delphi发短信接口

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdHTTP, IdURI, Httpapp;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function httpPost(postUrl:String;Params:TStrings):string;
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  url : string;
  username : string;
  password,apikey,mobile,content,encode,str : string;
  Params:   TStrings;
  i :Integer;
begin
  //乱码问题解决方案,1、GBK编码提交的首先urlencode短信内容(content),然后在API请求时,带入encode=gbk
  //2、UTF-8编码的将content 做urlencode编码后,带入encode=utf8或utf-8
  //实例:http://m.5c.com.cn/api/send/index.php?username=XXX&password=XXX&apikey=XXX&mobile=XXX&content=%E4%BD%A0%E5%A5%BD%E6%89%8D%E6%94%B6%E7%9B%8A%E9%9F%A6&encode=utf8
  Params   :=   TStringList.Create;
  encode := 'UTF-8';
  username := ''; //用户名
  password_md5 := ''; //32位MD5密码加密,不区分大小写
  apikey := ''; //apikey秘钥(请登录 http://m.5c.com.cn 短信平台-->账号管理-->我的信息 中复制
  mobile :=''; //手机号,只发一个号码:13800000001。发多个号码:13800000001,13800000002,...N 。使用半角逗号分隔。
  content := HttpEncode(AnsiToUtf8('验证码是:123【美联】')); //要发送的短信内容,特别注意:签名必须设置,网页验证码应用需要加添加【图形识别码】以防被短信攻击
  //ShowMessage(content);
  params.Add('encode='+encode);  //页面编码和短信内容编码为GBK。重要说明:如提交短信后收到乱码,请将GBK改为UTF-8测试。如本程序页面为编码格式为:ASCII/GB2312/GBK则该处为GBK。如本页面编码为UTF-8或需要支持繁体,阿拉伯文等Unicode,请将此处写为:UTF-8
  Params.Add('username='+username) ;  //用户名
  Params.Add('password_md5='+password_md5) ;  //密码
  Params.Add('apikey='+apikey) ;  
  Params.Add('mobile='+mobile) ;  
  Params.Add('content='+content) ;
  url := 'http://m.5c.com.cn/api/send/index.php?'; //如连接超时,可能是您服务器不支持域名解析,请将下面连接中的:【m.5c.com.cn】修改为IP:【115.28.23.78】
  ShowMessage(httpPost(url,Params));  //要发送的URL链接与内容。
  Params.Free;
end;
function  TForm1.httpPost(postUrl:string;Params:TStrings):string;
var
  idhtp1: TIdHTTP;
begin
  idhtp1:=   TidHTTp.create(self);
  idhtp1.AllowCookies:=True;
  idhtp1.HTTPOptions:=[hoForceEncodeParams];
  idhtp1.ProtocolVersion:=pv1_1;
  idhtp1.Request.ContentType:='application/x-www-form-urlencoded';
  idhtp1.Request.CacheControl:='no-cache';
  idhtp1.Request.UserAgent:='User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1';
  idhtp1.Request.Accept:='Accept=textml,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  idhtp1.Request.AcceptEncoding:='Accept-Encoding=gzip,deflate';
  idhtp1.Request.AcceptCharSet:='Accept-Charset=gb2312,utf-8;q=0.7,*;q=0.7';
  idhtp1.Request.Connection:='Connection=keep-alive';
  try
    result := idhtp1.Post(postUrl,Params);
  except
    Result := 'error';

相关文章