WebClient & Proxy (407 Proxy authentication required)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Morten Snedker

    WebClient & Proxy (407 Proxy authentication required)

    I'm trying to download a file. I've tried both using webclient and the
    httpWebRequest. Either way it returns a "407 proxy authentication
    required".

    The program is to be run externally, so basically I don't know the
    users login-name, password or domain. So that makes setting the proxy
    in code irrelevant, doesn't it?

    When searching the web it appears to be a common problem. But I
    haven't found my solution and still can't get it to work...

    This the code I'm trying to execute:

    '---
    Function DownloadVersion File() As Boolean

    LocalVersionfil e = Application.Exe cutablePath
    LocalVersionfil e = LocalVersionfil e.Substring(0,
    LocalVersionfil e.Length - 19) & "vk.txt"

    Dim wctl As New WebClient
    Try
    wctl.DownloadFi le(versionFile, LocalVersionfil e)
    DownloadVersion File = True
    Catch ex As Exception
    MsgBox(ex.Messa ge, MsgBoxStyle.Exc lamation, "Fejl i
    DownloadVersion File")
    DownloadVersion File = False
    End Try

    End Function
    '----


    Another part of my code that fails with same error:


    '---
    Dim myURI As Uri, url As String

    url = FileToDownload( )
    myURI = New Uri(url)

    Try
    Dim obj_HTTPWebRequ est As HttpWebRequest =
    CType(HttpWebRe quest.Create(ur l), HttpWebRequest)
    obj_HTTPWebRequ est.Credentials =
    CredentialCache .DefaultCredent ials
    Dim obj_HTTPWebResp onse As HttpWebResponse =
    obj_HTTPWebRequ est.GetResponse ()
    lFileSize = (obj_HTTPWebRes ponse.ContentLe ngth)
    Catch ex As Exception
    MsgBox(Err.Numb er)
    MsgBox(ex.Messa ge, MsgBoxStyle.Exc lamation, "Fejl i
    DownloadNewVers ionFile")
    Application.Exi t()
    End Try

    '---


    Thx in advance!

    Regards /Snedker
  • Chris Podmore

    #2
    RE: WebClient & Proxy (407 Proxy authentication required)

    I have tried your sample code here at work and I get the 407 as well. I think
    you are going to have to specify the proxy setting, I don't know of anyway
    round this.

    Try:

    Dim proxyObject As New System.Net.WebP roxy("http://999.999.999.999 :9999",
    True)
    obj_HTTPWebRequ est.Proxy = proxyObject
    obj_HTTPWebRequ est.Proxy.Crede ntials =
    System.Net.Cred entialCache.Def aultCredentials

    Replace 999.999.999.999 :9999 with your proxy IP address and port number.

    Hope this helps.
    Chris.


    "Morten Snedker" wrote:
    [color=blue]
    > I'm trying to download a file. I've tried both using webclient and the
    > httpWebRequest. Either way it returns a "407 proxy authentication
    > required".
    >
    > The program is to be run externally, so basically I don't know the
    > users login-name, password or domain. So that makes setting the proxy
    > in code irrelevant, doesn't it?
    >
    > When searching the web it appears to be a common problem. But I
    > haven't found my solution and still can't get it to work...
    >
    > This the code I'm trying to execute:
    >
    > '---
    > Function DownloadVersion File() As Boolean
    >
    > LocalVersionfil e = Application.Exe cutablePath
    > LocalVersionfil e = LocalVersionfil e.Substring(0,
    > LocalVersionfil e.Length - 19) & "vk.txt"
    >
    > Dim wctl As New WebClient
    > Try
    > wctl.DownloadFi le(versionFile, LocalVersionfil e)
    > DownloadVersion File = True
    > Catch ex As Exception
    > MsgBox(ex.Messa ge, MsgBoxStyle.Exc lamation, "Fejl i
    > DownloadVersion File")
    > DownloadVersion File = False
    > End Try
    >
    > End Function
    > '----
    >
    >
    > Another part of my code that fails with same error:
    >
    >
    > '---
    > Dim myURI As Uri, url As String
    >
    > url = FileToDownload( )
    > myURI = New Uri(url)
    >
    > Try
    > Dim obj_HTTPWebRequ est As HttpWebRequest =
    > CType(HttpWebRe quest.Create(ur l), HttpWebRequest)
    > obj_HTTPWebRequ est.Credentials =
    > CredentialCache .DefaultCredent ials
    > Dim obj_HTTPWebResp onse As HttpWebResponse =
    > obj_HTTPWebRequ est.GetResponse ()
    > lFileSize = (obj_HTTPWebRes ponse.ContentLe ngth)
    > Catch ex As Exception
    > MsgBox(Err.Numb er)
    > MsgBox(ex.Messa ge, MsgBoxStyle.Exc lamation, "Fejl i
    > DownloadNewVers ionFile")
    > Application.Exi t()
    > End Try
    >
    > '---
    >
    >
    > Thx in advance!
    >
    > Regards /Snedker
    >[/color]

    Comment

    • Morten Snedker

      #3
      Re: WebClient & Proxy (407 Proxy authentication required)

      On Thu, 25 Nov 2004 01:33:05 -0800, "Chris Podmore"
      <ChrisPodmore@d iscussions.micr osoft.com> wrote:

      First of all, thanks for your help so far. I've gotten a bit further
      since now I don't get any errors back from the server.

      But...it just won't download the file. If I run it at home it works
      fine, but from work through ISA and proxy, it doesn't.

      I've tried setting the proxy explicitly with
      Dim proxyObject As New System.Net.WebP roxy("http://PICASSO:8080",
      True), but this doesn't help either.

      Trying to read the filesize with ContentLength, -1 is returned. This
      is the code:

      Dim myURI As Uri, url As String
      url = versionFile
      myURI = New Uri(url)

      LocalVersionfil e = Application.Exe cutablePath
      LocalVersionfil e = LocalVersionfil e.Substring(0,
      LocalVersionfil e.Length - 19) & "vk.txt"

      Try
      Dim proxyObject As New WebProxy
      proxyObject.Get DefaultProxy()

      Dim obj_HTTPWebRequ est As HttpWebRequest =
      CType(HttpWebRe quest.Create(ur l), HttpWebRequest)

      With obj_HTTPWebRequ est
      .Proxy = proxyObject
      .Proxy.Credenti als =
      CredentialCache .DefaultCredent ials()
      Dim obj_HTTPWebResp onse As HttpWebResponse =
      ..GetResponse()
      lFileSize = (.ContentLength )
      End With

      Catch ex As Exception
      MsgBox(ex.Messa ge)
      Application.Exi t()
      End Try

      MsgBox("Filesiz e: " & lFileSize)


      Thanks again. Your help is greatly appreciated!

      /Snedker

      Comment

      • Chris Podmore

        #4
        Re: WebClient &amp; Proxy (407 Proxy authentication required)

        I'm at a bit of a loss as to what your problem is now. We have ISA etc and
        the following code works fine for me.

        Sorry I can't be more help.
        Chris.

        Dim url As String = "http://www.microsoft.c om"
        Dim obj_HTTPWebRequ est As HttpWebRequest = CType(HttpWebRe quest.Create(ur l),
        HttpWebRequest)

        'Remove these two lines to get 407 error
        obj_HTTPWebRequ est.Proxy = System.Net.WebP roxy.GetDefault Proxy
        obj_HTTPWebRequ est.Proxy.Crede ntials =
        System.Net.Cred entialCache.Def aultCredentials

        Dim r As WebResponse = obj_HTTPWebRequ est.GetResponse ()

        MsgBox(r.Conten tLength.ToStrin g)


        "Morten Snedker" wrote:
        [color=blue]
        > On Thu, 25 Nov 2004 01:33:05 -0800, "Chris Podmore"
        > <ChrisPodmore@d iscussions.micr osoft.com> wrote:
        >
        > First of all, thanks for your help so far. I've gotten a bit further
        > since now I don't get any errors back from the server.
        >
        > But...it just won't download the file. If I run it at home it works
        > fine, but from work through ISA and proxy, it doesn't.
        >
        > I've tried setting the proxy explicitly with
        > Dim proxyObject As New System.Net.WebP roxy("http://PICASSO:8080",
        > True), but this doesn't help either.
        >
        > Trying to read the filesize with ContentLength, -1 is returned. This
        > is the code:
        >
        > Dim myURI As Uri, url As String
        > url = versionFile
        > myURI = New Uri(url)
        >
        > LocalVersionfil e = Application.Exe cutablePath
        > LocalVersionfil e = LocalVersionfil e.Substring(0,
        > LocalVersionfil e.Length - 19) & "vk.txt"
        >
        > Try
        > Dim proxyObject As New WebProxy
        > proxyObject.Get DefaultProxy()
        >
        > Dim obj_HTTPWebRequ est As HttpWebRequest =
        > CType(HttpWebRe quest.Create(ur l), HttpWebRequest)
        >
        > With obj_HTTPWebRequ est
        > .Proxy = proxyObject
        > .Proxy.Credenti als =
        > CredentialCache .DefaultCredent ials()
        > Dim obj_HTTPWebResp onse As HttpWebResponse =
        > ..GetResponse()
        > lFileSize = (.ContentLength )
        > End With
        >
        > Catch ex As Exception
        > MsgBox(ex.Messa ge)
        > Application.Exi t()
        > End Try
        >
        > MsgBox("Filesiz e: " & lFileSize)
        >
        >
        > Thanks again. Your help is greatly appreciated!
        >
        > /Snedker
        >[/color]

        Comment

        • Morten Snedker

          #5
          Re: WebClient &amp; Proxy (407 Proxy authentication required)

          On Thu, 25 Nov 2004 03:43:03 -0800, "Chris Podmore"
          <ChrisPodmore@d iscussions.micr osoft.com> wrote:
          [color=blue]
          >I'm at a bit of a loss as to what your problem is now. We have ISA etc and
          >the following code works fine for me.[/color]
          [color=blue]
          >Sorry I can't be more help.[/color]

          Don't be - you've helped plenty !! :-)
          [color=blue]
          >Dim r As WebResponse = obj_HTTPWebRequ est.GetResponse ()[/color]

          I had
          Dim r As HttpWebResponse = .GetResponse()
          which is why it didn't work.

          But with your help in regards to the proxy, and my little typo, I'm on
          the right track...and all is working!

          Thanks a lot!


          Mr. Happy-Dude :-)

          Comment

          Working...