indy10的 TIdHTTPServer 和客户端TIdHTTP控件
//抓取服务器 的TIdHTTPServer初始化和侦听函;
1 procedure TMainForm.SetServerHostAndPort; 2 var 3 Binding : TIdSocketHandle; 4 begin 5 idhtpsrvr1.Bindings.Clear; 6 //1.绑定IP地址和端口 7 idhtpsrvr1.DefaultPort := 80; //若有固定绑定端口参数,一定要写这个 8 Binding := idhtpsrvr1.Bindings.Add; 9 Binding.IP := sIP; //本服务的IP 10 Binding.Port := sPort; //本服务的端口 11 end; 12 13 //guoqiang.xu 开启http监听 14 try 15 SetServerHostAndPort; 16 idhtpsrvr1.Active := True; 17 if idhtpsrvr1.Active then 18 LogInfo('监听服务器启动成功!', true) 19 else 20 LogInfo('监听服务器启动失败!', true); 21 except on E : Exception do 22 begin 23 LogInfo('监听服务器启动失败!'+ E.Message, true); 24 end; 25 end; 26 // 27 LogInfo('初始化成功', True);
1 procedure TMainForm.idhtpsrvr1CommandGet(AContext: TIdContext; 2 ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 3 var 4 sReqContext : string; 5 begin 6 try 7 //Post方式获取请求内容 8 if ARequestInfo.Command = 'POST' then 9 begin 10 //获取请求串 11 sReqContext := ARequestInfo.FormParams; 12 // 请求串为空,退出 13 if Trim(sReqContext) = '' then 14 Exit; 15 //解析请求串 16 //ParseRequest(sReqContext); 17 //处理请求 18 //GetReply(sReqContext); 19 end 20 else 21 begin 22 //获取请求串 23 sReqContext := ARequestInfo.QueryParams; 24 // 请求串为空,退出 25 if Trim(sReqContext) = '' then Exit; 26 //处理请求 27 GetReply(sReqContext); 28 Exit; 29 end; 30 except on E: Exception do 31 begin 32 LogInfo('【TOMis客户端手动请求获取竞彩对阵异常!】,异常原因:' + e.Message, True); 33 Exit; 34 end; 35 end; 36 end;
1 function TMainForm.GetReply(ARequest: string): Integer; 2 var 3 WholeParams: TStringList; 4 Msg_UserName, Msg_Code, Msg_Sign : string; 5 sReqStr,sTemp : string; 6 begin 7 Result := 0; 8 WholeParams := TStringList.Create; 9 try 10 Pub_GetMatch.Enter; 11 try 12 Msg_UserName := 'ReqUser'; 13 Msg_Code := 'ReqCode'; 14 Msg_Sign := 'aSign'; 15 WholeParams.Delimiter := '&'; 16 WholeParams.DelimitedText := ARequest; 17 sReqUserName := Trim(WholeParams.Values[Msg_UserName]); 18 sReqCode := Trim(WholeParams.Values[Msg_Code]); 19 sReqSign := Trim(WholeParams.Values[Msg_Sign]); 20 // 21 sReqStr := Trim(sReqUserName) + Trim(sReqCode) + 'aicai123456!'; // 公共密钥 22 sTemp := MD5String2(sReqStr); 23 if sTemp = sReqSign then 24 begin 25 case StrToInt(sReqCode) of 26 //竞足对阵(停赛对阵) 竞篮对阵 27 7,4 : 28 begin 29 try 30 Hand_JCMatchThd := TJCMatchThd.Create(StrToInt(sReqCode),0); 31 finally 32 end; 33 end; 34 //竞足竞篮赛果 35 70,45 : 36 begin 37 begin 38 try 39 Hand_JCResultThd := TJCResultThd.Create(StrToInt(sReqCode),0); 40 finally 41 end; 42 end; 43 end; 44 //北单对阵 45 3 : 46 begin 47 try 48 Hand_BDMatchThd := TBDMatchThd.Create(3,0); 49 finally 50 end; 51 end; 52 //北单赛果 300 为获取全部 53 30,31,32,33,34,300 : 54 begin 55 try 56 Hand_BDResultThd := TBDResultThd.create(DoNavigator,DoParse,StrToInt(sReqCode),0); 57 finally 58 end; 59 end; 60 //北单胜负对阵 61 35 : 62 begin 63 try 64 Hand_BDSFMatchThd := TBDSFMatchThd.Create(35,0); 65 finally 66 end; 67 end; 68 //北单胜负赛果 69 350 : 70 begin 71 try 72 Hand_BDSFResultThd := TBDSFResultThd.Create(350,0); 73 finally 74 end; 75 end; 76 else 77 Exit; 78 end; 79 end 80 else 81 begin 82 LogInfo('【手动请求获取竞彩对阵或赛果,校验签名不一样,终止请求!】' + sReqSign, True); 83 Exit; 84 end; 85 except on Ex: Exception do 86 begin 87 LogInfo('【手动请求获取竞彩对阵或赛果,请求参数异常!】,异常原因:' + Ex.Message, True); 88 Exit; 89 end; 90 end; 91 finally 92 WholeParams.Free; 93 Pub_GetMatch.Leave; 94 end; 95 end;
1 procedure TFrm010515.btnGetBDSFMatchClick(Sender: TObject); 2 var 3 ReqUrl, sReqSign, sReqKey : string; 4 Param : TStringList; 5 RStream : TStringStream; 6 sMsg : string; 7 idhtp1: TIdHTTP; 8 begin 9 Param:=TStringList.Create; 10 RStream:=TStringStream.Create(''); 11 idhtp1 := TIdHTTP.Create(nil); 12 try 13 try 14 sReqCode := '35'; //2014-09-25 liping.chen 暂时定义为35 待抓取服务器相应修改 15 sMsg := '【北单胜负对阵】'; 16 //生成签名 17 sReqSign := MD5String2(Pub_UserAccount + sReqCode + PubReqKey); 18 19 // http://127.0.0.1:2234/ReqUser=Pub_UserAccount#ReqCode=type#asign=sReqSign/ 20 // ReqUrl := 'http://' + Pub_ReqHost + ':' + IntToStr(Pub_Port) + '/' + 'ReqUser=' + Pub_UserAccount + '&ReqCode=' + sReqCode + '&aSign=' + sReqSign ; 21 //sRes := idhtp1.Get(ReqUrl); 22 //ReqUrl := 'HTTP://' + Pub_ReqHost + ':' + IntToStr(Pub_Port) + '/?' ; 23 ReqUrl := 'HTTP://' + Pub_ReqHost + ':' + IntToStr(Pub_Port) + '/getMatch?' ; 24 Param.Add('ReqUser=' + Pub_UserAccount); 25 Param.Add('ReqCode=' + sReqCode); 26 Param.Add('aSign=' + sReqSign); 27 //idhtp1.Post(Trim(ReqUrl), Param, RStream); 28 ReqUrl := ReqUrl + 'ReqUser=' + Pub_UserAccount + '&' + 'ReqCode=' + sReqCode + '&' + 'aSign=' + sReqSign; 29 idhtp1.Get(Trim(ReqUrl)); 30 //sMsn := RStream.DataString; 31 MessageboxCE(sMsg + '抓取指令发送成功,请过几分钟后查看赛程!', mtInformation, btOK); 32 except on e:Exception do 33 begin 34 MessageboxCE(sMsg + '抓取指令发送异常!原因为:' + e.Message + ReqUrl, mtInformation, btOK); 35 Exit; 36 end; 37 end; 38 finally 39 Param.Free; 40 RStream.Free; 41 idhtp1.Free; 42 end; 43 end; 44 45 procedure TFrm010515.btnGetBDSFResultClick(Sender: TObject); 46 var 47 ReqUrl,sReqSign: string; 48 sMsg : string; 49 idhtp1: TIdHTTP; 50 begin 51 idhtp1 := TIdHTTP.Create(nil); 52 try 53 try 54 sReqCode := '350'; //2014-09-25 liping.chen 暂时定义为350 待抓取服务器相应修改 55 sMsg := '【北单胜负赛果】'; 56 //生成签名 57 sReqSign := MD5String2(Pub_UserAccount + sReqCode + pubReqKey); 58 ReqUrl := 'HTTP://' + Pub_ReqHost + ':' + IntToStr(Pub_Port) + '/getMatch?' ; 59 ReqUrl := ReqUrl + 'ReqUser=' + Pub_UserAccount + '&' + 'ReqCode=' + sReqCode + '&' + 'aSign=' + sReqSign; 60 idhtp1.Get(Trim(ReqUrl)); 61 MessageboxCE(sMsg + '抓取指令发送成功,请过几分钟后查看赛程!', mtInformation, btOK); 62 except on e:Exception do 63 begin 64 MessageboxCE(sMsg + '抓取指令发送异常!原因为:' + e.Message + ReqUrl, mtInformation, btOK); 65 Exit; 66 end; 67 end; 68 finally 69 idhtp1.Free; 70 end; 71 end;
//开奖服务器 的TIdHTTPServer初始化和侦听函;
1 function TServerForm.StartHttpServer: Boolean; 2 begin 3 Result := False; 4 try 5 ServerDataModule.Draw_IdHTTPServer.Bindings.Clear; 6 with ServerDataModule.Draw_IdHTTPServer.Bindings.Add do 7 begin 8 IP := Pub_DrawIp; 9 Port := Pub_DrawPort; 10 end; 11 ServerDataModule.Draw_IdHTTPServer.Active := True; 12 {with ServerDataModule.Draw_IdHTTPServer do 13 begin 14 MaxConnections := 1; 15 StartListening; 16 end; } 17 LogMsg('已成功监听端口' + IntToStr(Pub_DrawPort), false); 18 Result := True; 19 except on Ex : Exception do 20 begin 21 LogMsg('监听端口' + IntToStr(Pub_BandPort) + '失败,原因:' + Ex.Message, false); 22 Exit; 23 end; 24 end; 25 end; 26 27 if not StartHttpServer then //启动监听线程 28 begin 29 LogMsg('启动监听线程失败。', false); 30 Exit; 31 end;
1 procedure TServerDataModule.Draw_IdHTTPServerCommandGet( 2 AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; 3 AResponseInfo: TIdHTTPResponseInfo); 4 var 5 ReceiveTime : TDateTime; 6 sRequestIP : string; 7 sRequestDoc :string; 8 begin 9 sRequestIP := ''; 10 ReceiveTime := Now; 11 LogMsg(Format('收到%s请求......................', [ARequestInfo.Command]), False, True); 12 //获取请求IP 13 sRequestIP := AContext.Connection.Socket.Binding.PeerIP; //记录请求串 14 // 暂时不保存 15 sRequestDoc := ARequestInfo.Document; 16 if not (pos('HttpDraw',sRequestDoc) > 0) then 17 begin 18 AResponseInfo.ContentText := '请求格式有误,请参照:http://127.0.0.1:80/HttpDraw?aDrawType=0&aLotMatType=1&aTermMatch=120205'; 19 Exit; 20 end; 21 // 请求串为空时,需要处理 22 if Trim(ARequestInfo.Params.Text) = '' then 23 begin 24 AResponseInfo.ContentText := '请求串为空'; 25 Exit; 26 end; 27 // 判断是否正在请求 28 if bIsRequesting then 29 begin 30 AResponseInfo.ResponseNo := 500; 31 AResponseInfo.ContentText := '开奖服务器繁忙,请稍候重试!'; 32 LogMsg('开奖服务器繁忙。', True, True); 33 end 34 else 35 begin 36 bIsRequesting := True; 37 AResponseInfo.ResponseNo := 200; 38 AResponseInfo.ContentText := DoDrawByHttp(ARequestInfo.Params.Text); 39 bIsRequesting := False; 40 end; 41 LogMsg('本次处理耗时:' + IntToStr(MilliSecondsBetween(Now, ReceiveTime)) + ' ms', False, True); 42 end;
1 function DoDrawByHttp(ARequest : string) : string; 2 var 3 sDrawType,sLotMatType,sTermMatch: string; 4 ReturnCode : integer; 5 ret : Boolean; 6 begin 7 ret := False; 8 ParseRequest(ARequest, sDrawType, sLotMatType, sTermMatch); 9 case StrToInt(sDrawType) of 10 0 : begin 11 ret := DoDraw(StrToInt(sLotMatType), sTermMatch, ReturnCode); 12 end; 13 1 : begin 14 if sLotMatType = '3' then 15 ret := DoDrawByBDTerm(sTermMatch, ReturnCode) 16 else 17 ret := DoDrawByMatch(StrToInt(sLotMatType), sTermMatch, ReturnCode); 18 end; 19 end; 20 // 21 if ret then 22 result := '开奖成功。' 23 else 24 result := '开奖失败。'; 25 end;
//投注接口服务器
1 function TFrmMain.StartHttpServer: Boolean; 2 begin 3 Result := False; 4 try 5 with dmMain.Main_IdHTTPServer.Bindings.Add do 6 begin 7 IP := Pub_BandIP; 8 Port := Pub_BandPort; 9 end; 10 with dmMain.Main_IdHTTPServer do 11 begin 12 MaxConnections := Pub_ListenThreadCount; 13 StartListening; 14 end; 15 SysLog(False, '已成功监听端口' + IntToStr(Pub_BandPort)); 16 Result := True; 17 except on E : Exception do 18 begin 19 SysLog(True, '监听端口' + IntToStr(Pub_BandPort) + '失败,原因:' + E.Message); 20 end; 21 end; 22 // Result := Start_HttpServer(Pub_BandIP, Pub_BandPort, Pub_ListenThreadCount, @GetReply, @SysLog, HttpServer, Pub_SysDBConStr); 23 end;
1 procedure TdmMain.Main_IdHTTPServerCommandGet(AContext: TIdContext; 2 ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 3 var 4 ReceiveTime : TDateTime; 5 TempConn: TBorrowConnection; 6 sRequestIP : string; //请求IP 2012-02-06 rulinzhang add 7 sReqStr :string; // 2013-03-12 rulinzhang add 8 begin 9 sRequestIP := ''; 10 ReceiveTime := Now; 11 //LogMsg(Format('收到%s请求......................', [ARequestInfo.Command]), False, True); 12 // 1 获取请求IP 13 sRequestIP := AContext.Connection.Socket.Binding.PeerIP; 14 // 2 获取请求串 15 sReqStr := ARequestInfo.Params.Text; 16 // 2012-03-27 rulinzhang add 请求串为空时,需要处理 2013-04-06 rulinzhang delete 17 {if (sRequestIP = '113.105.93.120') or (sRequestIP = '58.61.154.159') or (sRequestIP = '58.61.154.164') then 18 sReqStr := '本站网络监控。'; } 19 if Trim(sReqStr) = '' then 20 begin 21 sReqStr := Format('收到%s请求内容为空。', [ARequestInfo.Command]); 22 WriteRequestInfo(sReqStr,sRequestIP); 23 Exit; 24 end; 25 // 26 LogMsg(Format('收到%s请求......................', [ARequestInfo.Command]), False, True); 27 //记录请求串 28 WriteRequestInfo(sReqStr,sRequestIP); 29 // 30 TempConn := dmMain.GetConnection; 31 if TempConn.Connection = nil then //如果数据库连接耗尽 32 begin 33 AResponseInfo.ResponseNo := 500; 34 AResponseInfo.ContentText := '投注服务器发生故障'; 35 LogMsg('服务器发生故障,无可用的数据库连接', True, True); 36 AddErrorLog(0, mtAgent, midAgentBet, '服务器发生故障,无可用的数据库连接'); 37 // 2014-03-31 38 FrmMain.bReStartSys := True; 39 end 40 else begin 41 AResponseInfo.ResponseNo := 200; 42 AResponseInfo.ContentText := GetReply(TempConn, @SysLog, ARequestInfo.Params.Text); 43 end; 44 LogMsg('本次处理耗时:' + IntToStr(MilliSecondsBetween(Now, ReceiveTime)) + ' ms', False, True); 45 end;
1 procedure TdmMain.Main_IdHTTPServerException(AContext: TIdContext; 2 AException: Exception); 3 begin 4 LogMsg('服务器发生错误:' + AException.Message, True, True); 5 AddErrorLog(0, mtAgent, midAgentBet, AException.Message); 6 // 2014-03-31 7 FrmMain.bReStartSys := True; 8 end;
1 procedure WriteRequestInfo(RequestString,RequestIP: string); 2 var 3 FileName: string; 4 RecordFile: Textfile; 5 begin 6 Pub_LogCriticalSection.Enter; 7 try 8 try 9 //判断是否存在请求日志,否则创建 10 if not DirectoryExists(Pub_APPPath + '\请求日志') then 11 CreateDir(Pub_APPPath + '\请求日志'); 12 //文件关联 新建并打开文件 13 FileName := Pub_APPPath + '\请求日志\' + FormatDateTime('yyyy.mm.dd', date()) + '.txt'; 14 AssignFile(RecordFile, FileName); 15 if not FileExists(FileName) then 16 Rewrite(RecordFile) 17 else begin 18 Reset(RecordFile); 19 Append(RecordFile); 20 end; 21 if (Trim(RequestString) = '') or (RequestString = null) then 22 begin 23 Writeln(RecordFile, FormatDateTime('yyyy.mm.dd HH:mm:ss zzz',now()) + '-----' + '请求IP为:'+ RequestIP + '; 请求串为空' + #13#10#13#10); 24 AddErrorLog(0, mtAgent, midAgentBet, '请求IP为:' + RequestIP + '; 收到请求串为空'); 25 end 26 else 27 begin 28 Writeln(RecordFile,'[' + trim(RequestIP) + ']' + FormatDateTime('yyyy.mm.dd HH:mm:ss zzz',now()) + '-----' + trim(RequestString) + #13#10#13#10); 29 end; 30 except on e : Exception do 31 begin 32 LogMsg('写入请求失败!原因:' + e.Message, True, True); 33 end; 34 end; 35 finally 36 try 37 //2011-11-16 rulinzhang 由于出现异常,使临界区无法离开,导致下次无法进行临界 38 CloseFile(RecordFile); 39 except 40 end; 41 Pub_LogCriticalSection.Leave; 42 end; 43 end;
1 function GetReply(AConn: TBorrowConnection; ASysLog : Pointer; ARequest: string): string; 2 var 3 iAgent, iAction: Integer; 4 sMsgID: string; 5 isSelect :Boolean; 6 iSleep :Integer; 7 begin 8 isSelect := False; 9 ParseRequest(ARequest, iAgent, iAction, sMsgID); 10 // 2014-04-12 rulinzhang add 11 iSleep := Random(10) + 1; 12 Sleep(iSleep); 13 //---------------------------校验交易码------------------------------ 14 if (not CheckAction(iAction)) or (iAction = 0) then 15 begin 16 Result := PackageResult(RST_INVALID_ACTION, sMsgID, GetResultMsg(RST_INVALID_ACTION), ''); 17 SysLog(True, '交易码校验失败!代理商编号为:' + IntToStr(iAgent) +',消息序列号为:' + sMsgID + ',交易码为:' + IntToStr(iAction)); 18 dmMain.ReturnConnection(AConn.ConnectionIndex); 19 Exit; 20 end; 21 //---------------------------校验代理商------------------------------ 22 if not CheckAgent(iAgent) then 23 begin 24 Result := PackageResult(RST_INVALID_AGENT, sMsgID, GetResultMsg(RST_INVALID_AGENT), ''); 25 SysLog(True, '代理商校验失败!代理商编号为:' + IntToStr(iAgent) +',消息序列号为:' + sMsgID); 26 dmMain.ReturnConnection(AConn.ConnectionIndex); 27 Exit; 28 end; 29 // ------------------------------------ 30 case iAction of 31 101 : //投注接口 32 begin 33 with TBaseBiz.Create(AConn, ASysLog, ARequest) do 34 begin 35 try 36 Result := Execute; 37 finally 38 Free; 39 end; 40 end; 41 end; 42 103 : //出票状态查询接口 43 begin 44 if Pub_PrintQuery <> 1 then 45 begin 46 with TPrintQuery.Create(AConn, ASysLog, ARequest) do 47 begin 48 try 49 Result := Execute; 50 finally 51 Free; 52 end; 53 end; 54 end 55 else 56 isSelect := true; 57 end; 58 104 : //中奖信息查询接口 59 begin 60 if Pub_BingoQuery <> 1 then 61 begin 62 with TBingoQuery.Create(AConn, ASysLog, ARequest) do 63 begin 64 try 65 Result := Execute; 66 finally 67 Free; 68 end; 69 end; 70 end 71 else 72 isSelect := true; 73 end; 74 105 : // rulinzhang 2011-10-24 代理商余额查询接口 75 begin 76 if Pub_BalanceQuery <> 1 then 77 begin 78 with TBalanceQuery.Create(AConn,ASysLog,ARequest) do 79 begin 80 try 81 Result := Execute; 82 finally 83 Free; 84 end; 85 end; 86 end 87 else 88 isSelect := true; 89 end; 90 106 : // rulinzhang 2012-9-24 开奖结果查询接口 91 begin 92 if Pub_DrawQuery <> 1 then 93 begin 94 with TDrawResultQuery.Create(AConn,ASysLog,ARequest) do 95 begin 96 try 97 Result := Execute; 98 finally 99 Free; 100 end; 101 end; 102 end 103 else 104 isSelect := true; 105 end; 106 107 : // rulinzhang 2014-04-12 竞彩赛果查询接口 107 begin 108 if Pub_JCMatchQuery <> 1 then 109 begin 110 with TJCMatchQuery.Create(AConn,ASysLog,ARequest) do 111 begin 112 try 113 Result := Execute; 114 finally 115 Free; 116 end; 117 end; 118 end 119 else 120 isSelect := true; 121 end; 122 else 123 begin 124 Result := PackageResult(RST_INVALID_ACTION, sMsgID, GetResultMsg(RST_INVALID_ACTION), ''); 125 SysLog(True, '交易码校验失败!代理商编号为:' + IntToStr(iAgent) +',消息序列号为:' + sMsgID + ',交易码为:' + IntToStr(iAction)); 126 dmMain.ReturnConnection(AConn.ConnectionIndex); 127 Exit; 128 end; 129 end; 130 // 2013-11-19 rulinzhang add 131 if isSelect then 132 begin 133 Result := PackageResult(RST_REFUSE, sMsgID, GetResultMsg(RST_REFUSE), ''); 134 SysLog(True, '拒绝查询!代理商编号为:' + IntToStr(iAgent) +',消息序列号为:' + sMsgID + ',交易码为:' + IntToStr(iAction)); 135 dmMain.ReturnConnection(AConn.ConnectionIndex); 136 Exit; 137 end; 138 end;
程序关闭是要先关闭 dmMain.Main_IdHTTPServer.StopListening;

浙公网安备 33010602011771号