大家好,我在尝试自动化我们的 UPS 安装时遇到问题。该网页使用基本身份验证并在加载页面时提示登录。我们无权访问注册表以在 IE 中启用此功能,因为它已被禁用。我曾尝试使用 httpwebrequest 和 response 来拉取 cookie,但它似乎从来没有发回一个。我的逻辑是将该 cookie 用于 Web 浏览器控件,这样它就不会要求登录。这是我的代码:
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://10.106.206.249"), HttpWebRequest) Dim mycache = New CredentialCache() mycache.Add(New Uri("http://10.106.206.249"), "Basic", New NetworkCredential("User", "Pass")) request.Credentials = mycache request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14" request.CookieContainer = New CookieContainer() Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) Dim cook As Cookie For Each cook In response.Cookies Console.WriteLine("Cookie:") Console.WriteLine("{0} = {1}", cook.Name, cook.Value) Console.WriteLine("Domain: {0}", cook.Domain) Console.WriteLine("Path: {0}", cook.Path) Console.WriteLine("Port: {0}", cook.Port) Console.WriteLine("Secure: {0}", cook.Secure) Console.WriteLine("When issued: {0}", cook.TimeStamp) Console.WriteLine("Expires: {0} (expired? {1})", cook.Expires, cook.Expired) Console.WriteLine("Don't save: {0}", cook.Discard) Console.WriteLine("Comment: {0}", cook.Comment) Console.WriteLine("Uri for comments: {0}", cook.CommentUri) Console.WriteLine("Version: RFC {0}", IIf(cook.Version = 1, "2109", "2965")) ' Show the string representation of the cookie. Console.WriteLine("String: {0}", cook.ToString()) Next cook
我知道这在某种程度上有效,因为如果我使用不正确的信用,我会抛出一个未经授权的错误。所以看起来要么我没有收到 cookie,要么没有发送一个。
我尝试过的另一种方法是发送带有常规 Web.Navigate 的标头,但这就像它正在加载页面并提示登录:
Dim authData
Dim authHeader As String
authData = System.Text.UnicodeEncoding.UTF8.GetBytes("User:Pass")
authHeader = "Authorization: Basic: " & System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("User:Pass")) & Chr(13) & Chr(10)
Web.Navigate("http://10.106.206.249", False, Nothing, authHeader)
任何人都知道我是否在这里做错了什么?