用XE10开发微信服务器接口

crownsoft 2016-03-09 07:13:20
因ERP系统需要与微信公众号做数据接口,现准备做一个中间服务器。
开发环境:XE10
使用控件:IdHTTPServer

因刚开发阶段,在路由路上直接做了测试机的80端口转发,申请微信公众测试号后,却一直配置失败。跟踪发现是服务器无法收到微信发来的GET请求。代码如下:
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['echostr'] <> '' then
begin
Memo1.Lines.Add(ARequestInfo.Params.Values['echostr']);
AResponseInfo.ContentType := 'text/html; charset=UTF-8';
AResponseInfo.ContentText := ARequestInfo.Params.Values['echostr'];
end;
end;

end.


特意在代码中设定了一个每次接收到请求后自动在Memo1增加一生文字,直接在IE中输入对应服务器IP(外网IP),服务器有对应响应,但在微信公众平台上按‘提交’,服务却无任何反应。问是到底出在哪里?
我分析了下,主要怀疑以下几方面:
1、微信设定中的URL是不是有特别要求?(我设的是:http://110.211.17.203)(虽然IP是动态的,但每次测试时都有到路由器上确认)
2、微信的请求信息无法访问我的服务器?(不可以用端口转发?)
3、服务器所用控件或方法不对?

一直试了好几天都不行,微信的服务器肯定没问题,问题到底出在哪里?坛里有没有兄弟做过类似项目,请指教指教,不胜感激!
...全文
2016 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
我看见佛 2016-10-20
  • 打赏
  • 举报
回复
我开发过两个版本,.NET和JAVA的。建议用.NET或JAVA方便阿。
leeyihan 2016-10-18
  • 打赏
  • 举报
回复
电信联通都是封杀掉你的80端口的,可倒霉WX必须要指定到80端口。无解。 打开80要向运营商备案的。
Devchenxip 2016-10-11
  • 打赏
  • 举报
回复
好像不能直接用公网ip,必须有域名。 我最近刚好在学习这个,我用域名是没有问题的。
Devchenxip 2016-03-10
  • 打赏
  • 举报
回复
一直在关注delphi,但是好多年没用delphi做过实际项目了。你把问题发到盒子问问,那边比较多同学一直在用。 bbs.2ccc.com。

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧