登录  /  注册

开发微信服务器接口的实例教程

Y2J
发布: 2017-05-10 13:56:29
原创
1716人浏览过

因erp系统需要与微信公众号做数据接口,现准备做一个中间服务器。
开发环境:xe10
使用控件:idhttpserver

因刚开发阶段,在路由路上直接做了测试机的80端口转发,申请微信公众测试号后,却一直配置失败。跟踪发现是服务器无法收到微信发来的get请求。代码如下:

Delphi/Pascal code?

unit Unit1;

interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
  IdComponent, IdCustomTCPServer, IdCustomHTTPServer, IdHTTPServer, IdContext,
  IdHashSHA, IdGlobal;
 
type
  TForm1 = class(TForm)
    IdHTTPServer1: TIdHTTPServer;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure IdHTTPServer1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  private
    { Private declarations }
  public
    { Public declarations }
    function SHA1(Input: String): String;
    function CheckSignature(ARequestInfo: TIdHTTPRequestInfo): boolean;
  end;
 
var
  Form1: TForm1;
Const
  Token = 'weixin';
 
implementation
 
{$R *.dfm}
 
function TForm1.SHA1(Input: String): String;
begin
  with TIdHashSHA1.Create do
  try
    Result := LowerCase(HashBytesAsHex(TidBytes(Bytesof(Input))));
  finally
    Free;
  end;
end;
 
 
function TForm1.CheckSignature(ARequestInfo: TIdHTTPRequestInfo): boolean;
var
  signature, timestamp, nonce, echostr: String;
  tmpstr: TStringList;
  temp: String;
begin
  tmpstr := TStringList.Create;
  try
    signature := ARequestInfo.Params.Values['signature'];
    timestamp := ARequestInfo.Params.Values['timestamp'];
    nonce := ARequestInfo.Params.Values['nonce'];
 
    echostr := ARequestInfo.Params.Values['echostr'];
    tmpstr.Add(Token);
    tmpstr.Add(timestamp);
    tmpstr.Add(nonce);
    tmpstr.Sort;
    temp := StringReplace(tmpstr.text, #13#10, '', [rfReplaceAll]);
    Result := SHA1(temp) = signature;
  finally
    tmpstr.Free;
  end;
end;
 
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  IdHTTPServer1.Active := True;
end;
 
procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  Memo1.Lines.Add('123');
  if CheckSignature(ARequestInfo) then
  if ARequestInfo.Params.Values[&#39;echostr&#39;] <> &#39;&#39; then
  begin
    Memo1.Lines.Add(ARequestInfo.Params.Values[&#39;echostr&#39;]);
    AResponseInfo.ContentType := &#39;text/html; charset=UTF-8&#39;;
    AResponseInfo.ContentText := ARequestInfo.Params.Values[&#39;echostr&#39;];
  end;
end;
 
end.
登录后复制

以上就是开发微信服务器接口的实例教程的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号