有没有哪位大侠使用DELPHI开发在自己的程序中发送和接收微信内容?如有,烦请赐教!不胜感激!
解决方案 »
- 程序自动运行的问题
- 如何根据jsp设定弹出窗口的大小?
- 继续提问,这个问题怎么解决。
- 如何在注册表中添加一个键值
- Delphi+Oracle9i求助!(在线等待!)
- delphi7 在创建数据库时按钮不能显示
- 能否控制dbgrid显示的行数啊????
- To SnakeGuo:感谢帮忙找到flowchart,这里是第三个300分!
- sql查询的日期时间比较问题
- 在使用table时可以使用BatchMove导数据,可是改成ADOTable,就无法使用BatchMove,我该怎么办?
- 面向接口编程问题
- Delphi class constructor 和 class destructor 在 BPL 中
响应微信服务器GET和POST过来的数据
不过建议是用PHP或ASP.NET来做
以前下的一个代码,不记得原作者了,向源作者表示感谢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;