简体   繁体   English

如何在TWebBrowser中始终加载新页面?

[英]How to always load a fresh page in TWebBrowser?

I am trying to load a web page into TWebBrowser using Browser.Navigate(URL). 我正在尝试使用Browser.Navigate(URL)将网页加载到TWebBrowser中。 However, the browser won't care that the page was updated online so as long I won't restart the program it won't show the new page. 但是,浏览器不会关心页面是否在线更新,所以只要我不重启程序就不会显示新页面。

A more concrete example: If I navigate to a web page that has an visitor counter (as image), the counter will increment. 一个更具体的例子:如果我导航到具有访问者计数器(作为图像)的网页,计数器将递增。 If I navigate away from that page and then I return (without using Back), the counter won't increment. 如果我离开该页面然后返回(不使用Back),计数器将不会递增。 In Firefox, it does increment. 在Firefox中,它会增加。

This is what I tried and won't work: 这是我尝试过的,不会起作用:

const
  DLCTL_PRAGMA_NO_CACHE = $00004000;

procedure TBrowserFrm.LoadURL(URL: string);
var
  Flag: OleVariant;
begin
  Flag:=DLCTL_PRAGMA_NO_CACHE;
  Browser.Navigate(URL, Flag);
end; 


procedure TBrowserFrm.LoadURL(URL: string);
var
Flags: OleVariant;
begin
 Flags := 'navNoHistory, navNoReadFromCache, navNoWriteToCache';
 Browser.navigate2(URL, Flags);
end;

Any idea on how to make the TWebBrowser load the real page? 有关如何使TWebBrowser加载真实页面的任何想法?

In VCL, TWebBrowser is a wrapper for Internet Explorer, specifically the IWebBrowser2 interface. 在VCL中, TWebBrowser是Internet Explorer的包装器,特别是IWebBrowser2接口。

DLCTL_PRAGMA_NO_CACHE is NOT a flag you can pass to Navigate2() . DLCTL_PRAGMA_NO_CACHE 不是您可以传递给Navigate2()的标志。 Read the documentation: 阅读文档:

SHDocVw.TWebBrowser SHDocVw.TWebBrowser

TWebBrowser wraps the IWebBrowser2 interface from Microsoft's Shell Doc Object and Control Library (SHDOCVW.DLL) to allow you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications. TWebBrowser从Microsoft的Shell Doc对象和控件库(SHDOCVW.DLL)中包装IWebBrowser2接口,以允许您创建自定义Web浏览应用程序或向应用程序添加Internet,文件和网络浏览,文档查看和数据下载功能。

IWebBrowser2::Navigate2 Method IWebBrowser2 :: Navigate2方法

Flags [in] A pointer to a VARIANT of type VT_I4 or VT_I2 that specifies a combination of the values defined by the BrowserNavConstants enumeration . Flags [in]指向VT_I4或VT_I2类型的VARIANT的指针,指定BrowserNavConstants枚举定义的值的组合。

BrowserNavConstants Enumerated Type BrowserNavConstants枚举类型

typedef enum BrowserNavConstants {
    navOpenInNewWindow = 0x1,
    navNoHistory = 0x2,
    navNoReadFromCache = 0x4,
    navNoWriteToCache = 0x8,
    navAllowAutosearch = 0x10,
    navBrowserBar = 0x20,
    navHyperlink = 0x40,
    navEnforceRestricted = 0x80,
    navNewWindowsManaged = 0x0100,
    navUntrustedForDownload = 0x0200,
    navTrustedForActiveX = 0x0400,
    navOpenInNewTab = 0x0800,
    navOpenInBackgroundTab = 0x1000,
    navKeepWordWheelText = 0x2000,
    navVirtualTab = 0x4000,
    navBlockRedirectsXDomain = 0x8000,
    navOpenNewForegroundTab = 0x10000
} BrowserNavConstants;

As you can see, DLCTL_PRAGMA_NO_CACHE is not on that list. 如您所见, DLCTL_PRAGMA_NO_CACHE不在该列表中。 It is actually a flag you specify as an output value when implementing a handler for the browser's DISPID_AMBIENT_DLCONTROL property. 它实际上是在为浏览器的DISPID_AMBIENT_DLCONTROL属性实现处理程序时指定为输出值的标志。 Read the MSDN documentation: 阅读MSDN文档:

WebBrowser Customization | WebBrowser定制| Controlling Download and Execution 控制下载和执行

The WebBrowser Control gives you control over what it downloads, displays, and executes. WebBrowser Control使您可以控制下载,显示和执行的内容。 To gain this control, you need to implement your host's IDispatch so it handles DISPID_AMBIENT_DLCONTROL. 要获得此控制,您需要实现主机的IDispatch,以便它处理DISPID_AMBIENT_DLCONTROL。 When the WebBrowser Control is instantiated, it will call your IDispatch::Invoke with this ID. 实例化WebBrowser控件时,它将使用此ID调用IDispatch :: Invoke。 Set pvarResult to a combination of following flags, using the bitwise OR operator, to indicate your preferences. 使用按位OR运算符将pvarResult设置为以下标志的组合,以指示您的首选项。
... ...
•DLCTL_RESYNCHRONIZE and DLCTL_PRAGMA_NO_CACHE: These flags cause cache refreshes. •DLCTL_RESYNCHRONIZE和DLCTL_PRAGMA_NO_CACHE:这些标志导致缓存刷新。 With DLCTL_RESYNCHRONIZE, the server will be asked for update status. 使用DLCTL_RESYNCHRONIZE,将要求服务器提供更新状态。 Cached files will be used if the server indicates that the cached information is up-to-date. 如果服务器指示缓存的信息是最新的,则将使用缓存文件。 With DLCTL_PRAGMA_NO_CACHE, files will be re-downloaded from the server regardless of the update status of the files. 使用DLCTL_PRAGMA_NO_CACHE,无论文件的更新状态如何,都将从服务器重新下载文件。
... ...

So you would have to implement a custom IDispatch object and hook it into IWebBrowser2 in order to use DLCTL_PRAGMA_NO_CACHE correctly. 因此,您必须实现自定义IDispatch对象并将其挂钩到IWebBrowser2才能正确使用DLCTL_PRAGMA_NO_CACHE

Alternatively, you might consider switching to TEmbeddedWB , which handles the browser customization for you, and has a DownloadOptions property that accepts DLCTL... flags, including DLCTL_PRAGMA_NO_CACHE . 或者,您可以考虑切换到TEmbeddedWB ,它为您处理浏览器自定义,并具有接受DLCTL...标志的DownloadOptions属性,包括DLCTL_PRAGMA_NO_CACHE

I suppose you have to use 4, not $00004000. 我想你必须使用4,而不是$ 00004000。

Since I use www.ghisler.com (counter is at bottom) I can use 因为我使用www.ghisler.com(计数器在底部)我可以使用

procedure TForm2.Button2Click(Sender: TObject);
var
  Flags: OLEVariant;

begin
  Flags:=4; //NavNoReadFromCache
  WebBrowser1.Navigate('http://www.ghisler.com/', Flags);
end;

and it works perfectly (Delphi XE7). 它完美无缺(Delphi XE7)。 I see TC main page, click on hyperlink then on Button2 again and counter is new. 我看到TC主页,再点击超链接然后再点击Button2,计数器是新的。 When I use Navigate(URL) only, counter is still the same. 当我仅使用Navigate(URL)时,计数器仍然是相同的。

Browser.EnableCaching:=False; Browser.EnableCaching:= FALSE;

Browser.Navigate; Browser.Navigate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM