1,593
社区成员
发帖
与我相关
我的任务
分享
procedure TWebModule1.WebModule1CheckSignatureAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
signature: string;
timestamp: string;
nonce: string;
echostr: string;
strs: TStringList;
tmpStr: string;
begin
signature := Request.QueryFields.Values['signature'];
timestamp := Request.QueryFields.Values['timestamp'];
nonce := Request.QueryFields.Values['nonce'];
echostr := Request.QueryFields.Values['echostr'];
strs := TStringList.Create;
strs.Add(Token);
strs.Add(timestamp);
strs.Add(nonce);
strs.Sort;
tmpStr := strs[0]+strs[1]+strs[2];
tmpStr := SHA1(tmpstr);
if tmpStr=signature then
Response.Content := echostr
else
Response.Content := '';
strs.Destroy;
end;
//默认的HTTP服务响应,用于检测HTTP服务是否正常
procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.Content := '<html><head><meta http-equiv="Content-Type" content="text/html; charset="GB2312" /></head><body>服务器正常哦</body></html>';
end;
//微信响应
procedure TWebModule1.WebModule1wechatAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
xmlRequest: IXMLDocument;
xmlResponse: IXMLDocument;
node: IXMLNode;
tmpContent: RawByteString;
fromUserName: string;
toUserName: string;
content: String;
msgType: string;
CoResult: Integer;
begin
tmpContent := Request.Content;
AddRequestContent(tmpContent);
//在多线程的应用中使用基于ActiveX的MSXML时,要执行以下语句
CoInitializeEx(Nil, COINIT_MULTITHREADED);
if not((CoResult = S_OK) or (CoResult = S_FALSE)) then
begin
addLog('Failed to initialize COM library.');
Response.Content := '';
Exit;
end;
//解释从微信推送过来的消息,本例只响应text消息
try
xmlRequest := NewXMLDocument;
AddLog('Create Request XMLDocument Object Ok.');
xmlRequest.XML.BeginUpdate;
xmlRequest.XML.Text := tmpContent;
xmlRequest.XML.EndUpdate;
AddLog('Set xmlRequest.Text from Request.Content Ok.');
xmlRequest.Active := True;
AddLog('Set xmlRequest Active Ok.');
toUserName := xmlRequest.DocumentElement.ChildNodes.Nodes['ToUserName'].NodeValue;
AddLog('ToUserName: ' + toUserName);
fromUserName := xmlRequest.DocumentElement.ChildNodes.Nodes['FromUserName'].NodeValue;
AddLog('FromUserName: ' + fromUserName);
msgType := xmlRequest.DocumentElement.ChildNodes.Nodes['MsgType'].NodeValue;
AddLog('MsgType: ' + msgType);
if msgType = 'text' then
begin
content := xmlRequest.DocumentElement.ChildNodes.Nodes['Content'].NodeValue;
AddLog('Content: ' + content);
end;
except
on E: Exception do Addlog(E.Message);
end;
xmlRequest.Active := False;
xmlRequest := nil;
//构造回复给发信人的消息,本例只针对回复text消息
try
xmlResponse := NewXMLDocument;
AddLog('Create Response XMLDocument Object Ok.');
xmlResponse.XML.Text := '<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[content]]></Content><FuncFlag>0</FuncFlag></xml>';
AddLog('Set xmlResponse.Text to Default Test-Message XML Ok.');
xmlResponse.Active := true;
AddLog('Set xmlResponse Active Ok.');
node := xmlResponse.DocumentElement.ChildNodes.Nodes['ToUserName'];
node.NodeValue := fromUserName;
AddLog('Set ToUserName to: '+fromUserName);
node := xmlResponse.DocumentElement.ChildNodes.Nodes['FromUserName'];
node.NodeValue := toUserName;
AddLog('Set FormUserName to: '+toUserName);
node := xmlResponse.DocumentElement.ChildNodes.Nodes['CreateTime'];
node.NodeValue := UnixTime(Now);
AddLog('Set CreateTime to: '+Node.NodeValue);
node := xmlResponse.DocumentElement.ChildNodes.Nodes['MsgType'];
node.NodeValue := 'text';
AddLog('Set MsgType to: '+Node.NodeValue);
node := xmlResponse.DocumentElement.ChildNodes.Nodes['Content'];
node.NodeValue := '你好!'#10'欢迎你';
AddLog('Set Content to: '+Node.NodeValue);
node := xmlResponse.DocumentElement.ChildNodes.Nodes['FuncFlag'];
node.NodeValue := 0;
AddLog('Set FuncFlag to: '+Node.NodeValue);
tmpContent := UTF8Encode(xmlResponse.XML.Text); //把消息转为UTF-8
Response.ContentType := 'text/html; charset=UTF-8'; //让Response以UTF-8格式回复到微信服务器
Response.Content := tmpContent;
AddResponseContent(':'+tmpContent);
except
on E: Exception do Addlog(E.Message);
end;
xmlResponse.Active := False;
xmlResponse := nil;
end;