有没有哪位开发过收发微信内容模块?

superlixiao78 2014-12-09 10:16:41
有没有哪位大侠使用DELPHI开发在自己的程序中发送和接收微信内容?

如有,烦请赐教!

不胜感激!
...全文
316 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
风吹来 2014-12-15
  • 打赏
  • 举报
回复
看上去好强大,貌似比php复杂多了
knife_s 2014-12-10
  • 打赏
  • 举报
回复
使用TIdHTTPWebBrokerBridge 响应微信服务器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;

1,593

社区成员

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

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