您好我有一个网站,我试图只为我的浏览器用户显示这个网站。 (我的浏览器也只显示我的网站)
所以我在visual basic express上编写了这个浏览器
Public Class Form1
Dim userClickNavigate As Boolean = True
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
userClickNavigate = True
End Sub
Private Sub WebBrowser1_Navigating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
If Not userClickNavigate Then
userClickNavigate = True
WebBrowser1.Navigate(e.Url, Nothing, Nothing, "User-Agent:Nokia-MIT-Browser/3.0")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
userClickNavigate = False
WebBrowser1.Navigate("http://orenaksakal.org/demo2/", Nothing, Nothing, "User-Agent:Nokia-MIT-Browser/3.0")
End Sub
End Class
这首先工作真的很好。但我有我的PHP文件。那段代码;
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
echo '<script type="text/javascript">alert("Bu site Internet Exploreri desteklemiyor!");</script>';
die();
当我刷新页面或转到下一个链接时,我的用户代理更改不再有效...转回IE ..
我怎么能一直为我的浏览器程序保留它?
答案 0 :(得分:0)
在BeforeNavigate2事件中,您可以拦截浏览器请求,取消它并将其替换为您自己的。您对Navigate2方法的调用将导致事件再次触发,因此您需要使用表单级别变量来检查和阻止递归。我不确定你用userClickNavigate标志做了什么,所以你可能需要修改它以使用它。
Option Explicit
Private Const USER_AGENT = "User-Agent:Nokia-MIT-Browser/3.0" 'contant declaration of use agent
Private mblnRecursing As Boolean ' form level variable to prevent recursion
Private Sub Form_Load()
WebBrowser1.Navigate2 "http://orenaksakal.org/demo2"
End Sub
Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
Dim MyHeaders As Variant
If (mblnRecursing = False) Then
mblnRecursing = True
Cancel = True
MyHeaders = Headers & USER_AGENT
WebBrowser1.Navigate2 URL, Flags, TargetFrameName, PostData, MyHeaders
End If
mblnRecursing = False
End Sub