Delphi RTCHttpClient + RtcDataRequest 访问网站

//一般可以在一开始就先连接网站。设置自动连接即可 
with RtcHttpClient do
 begin
  AutoConnect := True;
  ServerAddr := 'xxxx.com';
  ServerPort := '80';
  if not RtcHttpClient.isConnected then
   Connect();
 end;
//在 RtcDataRequest 的 DataRequestBeginRequest 事件写上 procedure RtcDataRequestBeginRequest(Sender: TRtcConnection); var RtcClient:TRtcDataClient absolute Sender; begin with RtcClient do begin IF Request.Params.Text <> '' THEN begin Write(Request.Params.Text); end else WriteHeader(''); end; end; //访问网站文件 每次访问不需要上面的代码。设置完后,每次直接在这里请求即可 with RtcDataRequest.Request do begin Close := True; Agent := AnsiString('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36'); Method:=AnsiString('POST'); ContentType := AnsiString('application/json'); FileName:=AnsiString(''); Host :=AnsiString(''); Cookie.asString['action'] := action; Params.Text := '{}'; end;
//数据响应 procedure TForm1.RtcDataRequest_DataReceived(Sender: TRtcConnection); var RtcClient:TRtcDataClient absolute Sender; ResponseHear, ResponseRead :string; begin //不在主线程内 则同步到主线程去处理。如果有处理到UI的话 if RtcClient.inMainThread then begin if RtcClient.Response.Done then begin //要先赋值到字符串去 不然用utf8解码 中文会有问题。 不能直接这样 ResponseRead := UTF8ToAnsi(RtcClient.Read); //虽然上面那样赋值看起来是正确的,但是通过下面的so就不对了。要切记先赋值到字符串去 ResponseRead := RtcClient.Read; //UTF8解码 ResponseRead := UTF8ToAnsi(ResponseRead); ResponseHear := RtcClient.Response.HeaderText; cxm_RTCINFO.Lines.Add('Request.Params:BEGIN----'); cxm_RTCINFO.Lines.Add(RtcClient.Request.Params.Text); cxm_RTCINFO.Lines.Add('Request.Params:END----'); cxm_RTCINFO.Lines.Add('ResponseHear:BEGIN----'); cxm_RTCINFO.Lines.Add(ResponseHear); cxm_RTCINFO.Lines.Add('ResponseHear:END----'); cxm_RTCINFO.Lines.Add('Red:BEGIN----'); cxm_RTCINFO.Lines.Add(ResponseRead); cxm_RTCINFO.Lines.Add('Red:END----'); end else RtcClient.Sync(RtcDataRequest_DataReceived); end; //退出时 关闭所有连接 这里参考的是rtc自带的demo RtcHttpClient.DisconnectNow(True);
原文地址:https://www.cnblogs.com/BTag/p/15699337.html