TIdHTTP、THttpCli、TIdHTTPServer
1、TIdHTTP
sResponse : string; idServer := TIdHTTP.Create(nil); try sResponse := UTF8Decode(idServer.Get(MainForm.BDMatchAddress)); //转换 UTF8Decode if MainForm.IsSaveFile then begin if MainForm.SaveFile(sResponse,'FileBDDZDate') then begin MainForm.LogInfo('保存北单对阵页面成功', True); end; end; except on E: Exception do begin MainForm.LogInfo('抓取北单对阵出错!', True); // AddErrorSmsLog(10,'【抓取北单赛程失败】' + '抓取北单对阵出错!'+ E.Message); Exit; end; //------------- idServer := TIdHTTP.Create(nil); try try MainForm.LogInfo('正在获取大乐透开奖结果', True); sResponse := Utf8ToAnsi(idServer.Get(MainForm.DLTAddress)); //一定要转换编码Utf8ToAnsi
//---------------------
sResponse := idHttp.Get(MainForm.FBMatchAddress); //不用转换
2、
HTTPClient: THttpCli;
HTTPClient:=THttpCli.create(nil);
function TAddLogThread.HttpRequest(const RequestType: TRequestType; const Url, Data: string; var Html: string): Boolean; var DataLen: Int64; //FailMsg: string; begin Result := False; HTTPClient.URL := Url; if RequestType = rtPost then begin HTTPClient.SendStream := TMemoryStream.Create; HTTPClient.SendStream.Write(Data[1], Length(Data)); HTTPClient.SendStream.Seek(0, 0); end; HTTPClient.NoCache := True; HTTPClient.RcvdStream := TMemoryStream.Create; try if RequestType = rtPost then HTTPClient.Post else HTTPClient.Get; try DataLen := HTTPClient.RcvdStream.Size; SetLength(Html, DataLen); HTTPClient.RcvdStream.Position := 0; HTTPClient.RcvdStream.Read(PChar(Html)^, DataLen); Result := True; except end; finally HTTPClient.SendStream.Free; HTTPClient.SendStream := nil; HTTPClient.RcvdStream.Free; HTTPClient.RcvdStream := nil; end; end;
线程中调用如下:
procedure TAddLogThread.Execute; //主线程 var i:integer; SubErrorList:TStringList; SubActionList:TStringList; subErrorSmsList : TStringList ; ErrorURL,ActionURL,ErrorSmsURL,temp:string; begin while not Terminated do begin try ErrorURL :='http://'+host+':'+inttostr(port)+'/Error.asp'; ActionURL :='http://'+host+':'+inttostr(port)+'/action.asp'; ErrorSmsURL := 'http://'+host+':'+inttostr(port)+'/needsms.asp'; // if (ErrorList.Count >0) then begin SubErrorList:=TStringList.Create ; try SubErrorList.Add(Account+'||'+PassWord); for i:=0 to ErrorList.Count-1 do begin SubErrorList.Add(ErrorList[0]); synchronize(DelErrorList); end; try HttpRequest(rtPost,ErrorURL,SubErrorList.Text,temp); except on e:exception do begin FLogMsg('向服务器发送错误日志出错!'#13+e.Message,true,true); end; end; finally SubErrorList.Free; end; end; if (ErrorSmsList.Count >0) then begin subErrorSmsList:=TStringList.Create ; try subErrorSmsList.Add(Account+'||'+PassWord); for i:=0 to ErrorSmsList.Count-1 do begin subErrorSmsList.Add(ErrorSmsList[0]); synchronize(DelErrorSmsList); end; try HttpRequest(rtPost,ErrorSmsURL,subErrorSmsList.Text,temp); except on e:exception do begin FLogMsg('向服务器发送错误日志出错!'#13+e.Message,true,true); end; end; finally subErrorSmsList.Free; end; end; if (ActionList.Count >0) then begin SubActionList:=TStringList.Create ; //创建 try SubActionList.Add(Account+'||'+PassWord); //添加验证信息 for i:=0 to ActionList.Count-1 do begin SubActionList.Add(ActionList[0]); synchronize(DelActionList); end; try HttpRequest(rtPost,ActionURL,SubActionList.Text,temp); except on e:exception do begin FLogMsg('向服务器发送运行日志出错!'#13+e.Message,true,true); end; end; finally SubActionList.Free; end; end; for i := 1 to FInterval do begin if not Terminated then Sleep(1000); end; except on e:exception do begin FLogMsg('线程Execute过程出错!'#13+e.Message,true,true); end; end; end; END;
3、idhtpsrvr1: TIdHTTPServer;
设定
procedure TMainForm.SetServerHostAndPort; var Binding : TIdSocketHandle; begin idhtpsrvr1.Bindings.Clear; //1.绑定IP地址和端口 idhtpsrvr1.DefaultPort := 80; //若有固定绑定端口参数,一定要写这个 Binding := idhtpsrvr1.Bindings.Add; Binding.IP := sIP; Binding.Port := sPort; end;
启动
// 开启http监听 try SetServerHostAndPort; idhtpsrvr1.Active := True; if idhtpsrvr1.Active then LogInfo('监听服务器启动成功!', true) else LogInfo('监听服务器启动失败!', true); except begin LogInfo('监听服务器启动失败!', true); end; end;
接收
procedure TMainForm.idhtpsrvr1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); var sReqContext : string; begin try //Post方式获取请求内容 if ARequestInfo.Command = 'POST' then begin //获取请求串 sReqContext := ARequestInfo.FormParams; // 请求串为空,退出 if Trim(sReqContext) = '' then Exit; //解析请求串 //ParseRequest(sReqContext); //处理请求 //GetReply(sReqContext); end else begin //获取请求串 sReqContext := ARequestInfo.QueryParams; // 请求串为空,退出 if Trim(sReqContext) = '' then Exit; //处理请求 GetReply(sReqContext); Exit; end; except on E: Exception do begin LogInfo('【TOMis客户端手动请求获取竞彩对阵异常!】,异常原因:' + e.Message, True); Exit; end; end; end;

浙公网安备 33010602011771号