Publish data using Indy and retrieve it in TWebBrowser

I am trying to send data to bing using this code

function PostExample: string; var lHTTP: TIdHTTP; lParamList: TStringList; begin lParamList := TStringList.Create; lParamList.Add('q=test'); lHTTP := TIdHTTP.Create(nil); try Result := lHTTP.Post('http://www.bing.com/', lParamList); finally FreeAndNil(lHTTP); FreeAndNil(lParamList); end; end; 

And then, how can I get the result in TWebBrowser and display it?

+4
source share
2 answers

Try LoadDocFromString :

 procedure LoadBlankDoc(ABrowser: TWebBrowser); begin ABrowser.Navigate('about:blank'); while ABrowser.ReadyState <> READYSTATE_COMPLETE do begin Application.ProcessMessages; Sleep(0); end; end; procedure CheckDocReady(ABrowser: TWebBrowser); begin if not Assigned(ABrowser.Document) then LoadBlankDoc(ABrowser); end; procedure LoadDocFromString(ABrowser: TWebBrowser; const HTMLString: wideString); var v: OleVariant; HTMLDocument: IHTMLDocument2; begin CheckDocReady(ABrowser); HTMLDocument := ABrowser.Document as IHTMLDocument2; v := VarArrayCreate([0, 0], varVariant); v[0] := HTMLString; HTMLDocument.Write(PSafeArray(TVarData(v).VArray)); HTMLDocument.Close; end; 
+7
source

Or you can use memory streams to load

 uses OleCtrls, SHDocVw, IdHTTP, ActiveX; function PostRequest(const AURL: string; const AParams: TStringList; const AWebBrowser: TWebBrowser): Boolean; var IdHTTP: TIdHTTP; Response: TMemoryStream; begin Result := True; try AWebBrowser.Navigate('about:blank'); while AWebBrowser.ReadyState < READYSTATE_COMPLETE do Application.ProcessMessages; Response := TMemoryStream.Create; try IdHTTP := TIdHTTP.Create(nil); try IdHTTP.Post(AURL, AParams, Response); if Response.Size > 0 then begin Response.Position := 0; (AWebBrowser.Document as IPersistStreamInit).Load( TStreamAdapter.Create(Response, soReference)); end; finally IdHTTP.Free; end; finally Response.Free; end; except Result := False; end; end; procedure TForm1.Button1Click(Sender: TObject); var Params: TStringList; begin Params := TStringList.Create; try Params.Add('q=test'); if not PostRequest('http://www.bing.com/', Params, WebBrowser1) then ShowMessage('An unexpected error occured!'); finally Params.Free; end; end; 
+4
source

Source: https://habr.com/ru/post/1396273/


All Articles