1. 程式人生 > 其它 >Delphi RTCHttpClient + RtcDataRequest 訪問網站

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);