VB.net error - Unable to cast object of type 'WebProxyWrapper' to type 'System.Net.WebProxy'.

  • Hi,

    I get an error when I run this code - Unable to cast object of type 'WebProxyWrapper' to type 'System.Net.WebProxy'.:

    ----------------------------------------------------------

    Dim restRequest As WebRequest

    Dim restResponse As HttpWebResponse

    Dim rXML As StreamReader

    Dim sXML As String

    Dim myProxy As New WebProxy

    myProxy = WebRequest.DefaultWebProxy() 'use default proxy setting

    restRequest = CType(WebRequest.Create("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"), WebRequest.DefaultWebProxy)

    restRequest.Proxy = WebRequest.DefaultWebProxy

    restRequest.Proxy.Credentials = CredentialCache.DefaultCredentials

    restResponse = DirectCast(restRequest.GetResponse(), HttpWebResponse)

    ---------------------------------------------------------------------------------------

    Does someone have any idea of what could be wrong ?

    Thanks,

    Paul

  • This line isn't required:

    myProxy = WebRequest.DefaultWebProxy() 'use default proxy setting

    You're setting the proxy directly two lines further down. Not sure what the CType is for as well. I knocked up this quick example from your code which works fine in a console app for me (I'm behind a proxy as well, so it must be setting it correctly):

    Dim restRequest As WebRequest

    Dim restResponse As HttpWebResponse

    Dim rXML As StreamReader

    Dim sXML As String

    restRequest = WebRequest.Create("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml")

    restRequest.Proxy = WebRequest.DefaultWebProxy

    restRequest.Proxy.Credentials = CredentialCache.DefaultCredentials

    restResponse = DirectCast(restRequest.GetResponse(), HttpWebResponse)

    Dim SR As StreamReader

    SR = New StreamReader(restResponse.GetResponseStream)

    Console.WriteLine(SR.ReadToEnd.ToString())

    FYI - I'm no expert, so you'd probably get a more best practice approach from a dedicated forum

  • Thanks a lot for your reply !

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply