我有一个从Intranet Web服务获取XML的Visual Basic应用程序,这很好用。但是,有一个页面需要Windows身份验证(由于安全性)。 Web服务在Apache Tomcat 7上运行 - 使用WAFFLE来使用Windows身份验证。这在Web浏览器中工作正常,Internet Explorer以静默方式登录用户。 (This is the method used)
以下是我用来从提供的字符串URL获取XML的代码。
Dim xmlResolver As XmlUrlResolver = New XmlUrlResolver()
WindowsIdentity.GetCurrent.Impersonate()
xmlResolver.Credentials = CredentialCache.DefaultCredentials
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.IgnoreComments = True
settings.XmlResolver = xmlResolver
Dim xmlReader As XmlReader = XmlReader.Create(sURL, settings)
Do While (xmlReader.Read())
//Parsing Code
Loop
xmlReader.Close()
这可以很好地获取未受保护页面的XML,但是在尝试从安全页面检索XML时出现以下异常:
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.FinishInitUriString()
at System.Xml.XmlTextReaderImpl..ctor(String uriStr, XmlReaderSettings settings, XmlParserContext context, XmlResolver uriResolver)
at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings)
at PublicWebServiceTester.frmAlarms.raiseAlarm() in ..\frmAlarms.vb:line 44
在我看来,用户没有正确通过。