使用 Indy 发布数据并将其接收到 TWebBrowser

发布于 2025-01-04 19:16:47 字数 419 浏览 1 评论 0原文

我正在尝试使用此代码将数据发布到 bing

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;

然后,如何将结果发送到 TWebBrowser 并显示它?

I am trying to post data to bing with 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 to the TWebBrowser and display it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2025-01-11 19:16:47

尝试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;

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;
农村范ル 2025-01-11 19:16:47

或者您可以使用内存流进行加载

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;

Or you can use the memory streams for loading

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文