program Project1;{$APPTYPE CONSOLE}uses
  SysUtils,SHDocVw,MSHTML;var
  iall : IHTMLElement;
  Webbrowser:TWebBrowser;
begin
  WebBrowser :=TWebBrowser.Create(nil);
  WebBrowser.Navigate('http://192.168.1.101/1.html');
  Sleep(1000);
  if Assigned(WebBrowser.Document) then
  begin
    iall := (WebBrowser.Document AS IHTMLDocument2).body;
    while iall.parentElement <> nil do
    begin
      iall := iall.parentElement;
    end;
    Writeln(iall.outerHTML);
  end;
  Readln;
  Webbrowser.Free;
end.
1.html里存放的是文本   "success=true"使用上边的代码无法正确输出success=true这一串字符而在VCL Forms Application 中则可以,请问是哪的原因?如何解决呢?

解决方案 »

  1.   

    If all you want is return result of some URL why do you need to embed whole webbrowser? if you just need the result then try Indy.retStr := IdHTTP.Get('http://192.168.1.101/1.html');Hope it helps.//Ali
      

  2.   


    我也知道IDHTTP简单的就可以办到,
    但在这里我必须使用Webbrowser获得它的源码,不好意思了
      

  3.   

    IdHTTP.Get will also return you whole HTML or 源码 of that link. Do you want to display the results as HTML Browser??//Ali
      

  4.   


    I know I can use IDHTTPBut,Procedures require the use of Webbrowser!Thans!
      

  5.   

    Now your requirements are clear. Use below code to retrieve HTML via webbrowser.
    procedure Pause(const ADelay: Cardinal);
    var
       Start: Cardinal;
    begin
        Start := Windows.GetTickCount;
        repeat
              Application.ProcessMessages;
        until Int64(Windows.GetTickCount) - Int64(Start) >= ADelay;
    end;function getHTML(WB : TWebBrowser) : String ;
     var
       PersistStream: IPersistStreamInit;
       Stream: IStream;
       strStream: TStringStream;
     begin
       Result := '';
       if Assigned(WB.Document) then
       begin
       PersistStream := WB.Document as IPersistStreamInit;
       strStream := TStringStream.Create('') ;
       try
         Stream := TStreamAdapter.Create(strStream, soReference) as IStream;
         if Succeeded(PersistStream.Save(Stream, True)) then
             Result := strStream.DataString;
       finally
         strStream.Free;
       end;
       end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
       browser : TWebBrowser;
       Flags : OleVariant;
    begin     Flags := navNoHistory or navNoReadFromCache or navNoWriteToCache;
         browser := TWebBrowser.Create(nil);
         try
           browser.Navigate(Edit1.Text,  Flags);
           while browser.Busy do
             Pause(5);
           memo1.Text := getHTML(browser);
         finally
               browser.Free;
         end;
    end;
    PS Calling sleep isn't a good option. That's why I implemented a Pause method which in turn keep calling Application.processMessages so that it doesn't give end user an image of application hangs.Hope it helps.
    //Ali
      

  6.   

    sleep used in the thread, in the application that I can get to the source code, but can not get in the console, please test your console good?
      

  7.   

    Definitely you can not have any TWinControl descendent in console environment. And plus I don't understand why would you go with WinControl in non-window (console) environment?//Ali
      

  8.   

    The problem is not resolved, Results posted!
      

  9.   

    楼主的问题解决了没有?我现在也遇到了动态创建的webbrowser,不能正解显示网页内容,我用
      while webbrowser.ReadyState < READYSTATE_COMPLETE do        Application.ProcessMessages;
    判断,ReadyState的值一直都是3,也就是没有加载完成,这是如果改为webbrowser.ReadyState < 3,可以向下执行就是显示的数据不正确
      

  10.   

    while webbrowser.ReadyState <> READYSTATE_COMPLETE do Application.ProcessMessages;你应该改成这样的!我的问题解决了.转换成创建APP程序.