XmlResolver doesn't authenticate with Proxy server

Just had a problem in resolving a DTD, stored at a remote URI, while loading an XML file into a XmlDocument. Usually a XmlUrlResolver is used to resolve remote XSD or DTD defined in an XML document, however the XmlUrlResolver doesn't perform Proxy authentication meaning that as soon as you try to load the document a WebException is raised saying "The remote server returned an error: (407) Proxy Authentication Required.". The solution is to create your own XmlResolver that handles proxy authentication and here it is.

Public Class XmlProxyUrlResolver
Inherits XmlResolver

Private m_proxy As IWebProxy = WebProxy.GetDefaultProxy()
Private m_credentials As ICredentials

Public Overrides WriteOnly Property Credentials() As System.Net.ICredentials
Set(ByVal value As System.Net.ICredentials)
m_credentials = value
End Set
End Property

Public Overrides Function GetEntity(ByVal absoluteUri As System.Uri, ByVal role As String, _
ByVal ofObjectToReturn As System.Type) As Object
Dim request As WebRequest = WebRequest.Create(absoluteUri)
request.Proxy = m_proxy
request.Proxy.Credentials = m_credentials
request.Credentials = m_credentials
Return request.GetResponse().GetResponseStream()
End Function

End Class


The XmlProxyUrlResolver resolves the DTD at http://www.w3.org with authenticating with the proxy server.

Published Tuesday, October 02, 2007 3:04 PM by dsmyth

Comments

No Comments