2

我正在使用 .net Web 浏览器控件通过具有凭据的代理在新窗口中打开一个 url。我为此使用此代码:

Public Function AppendHeader(ByRef OriginalHeader As String, ByVal Addition As String) As Boolean
    If OriginalHeader <> "" Then
        OriginalHeader = OriginalHeader + vbNewLine
    End If
    OriginalHeader = OriginalHeader + Addition
    OriginalHeader.Trim(vbNewLine.ToCharArray)
End Function

Public Function Base64Enc(ByRef s As String) As String
    Base64Enc = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(s))
End Function

Public Sub Navigate()
    Dim webBrowser As WebBrowser = New WebBrowser()
    Dim headers As String = ""
    AppendHeader(headers, "Proxy-Authorization: Basic " & Base64Enc("user:pass"))
    'AppendHeader(headers, "Authorization: Basic " & Base64Enc("user:pass"))
    webBrowser.Navigate("http://stackoverflow.com", Guid.NewGuid().ToString(), Nothing, headers)
End Sub

此代码有助于在第一次隐藏 Windows 安全窗口,但如果加载网页将请求发送到其他 url,则此窗口会一次又一次地显示(您可以在下面的屏幕截图中看到它)。

截屏

那么我能做些什么来解决这个问题呢?(我使用的是 winforms 和 vb.net,但 C# 也适用)

4

1 回答 1

1

试一试我无法测试它:)

Private Sub webBrowser_Navigating(ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs)

    Dim credentials As New System.Net.NetworkCredential("user", "pwd", "MyDomain") 
    Dim proxy As New System.Net.WebProxy("127.0.1.2", 80) 
    If proxy Is Nothing Then
        e.Cancel = True
    End If

End Sub
于 2015-08-01T06:46:51.097 回答