Results 1 to 10 of 10

Thread: WebClient error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    395

    WebClient error

    I am getting an error with this code:

    Code:
    Dim Web As New WebClient
    Web.Proxy = "69.196.16.237:62159"
    It says runtime errors may occur when converting string to System.Net.IWebProxy

    What does that mean?

    Thanks,

    Jon

  2. #2
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: WebClient error

    Try
    Code:
            Dim Web As New WebClient
            Web.Proxy = New WebProxy("69.196.16.237", 62159)

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    395

    Re: WebClient error

    That worked nicely thanks! My code is now:

    Code:
    Dim Web As New WebClient
    Web.Proxy = New WebProxy("69.196.16.237", 62159)
    Dim Source As String = Web.DownloadString("http://google.com")
    How can I check for the download either timing out or the website refusing a connection?

  4. #4
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: WebClient error

    The WebClient class Automatically throws Exceptions for those two issues.
    Code:
            Try
                Dim source As String = Web.DownloadString("http://www.thiscannotpossiblyexsist")
            Catch ex As System.Net.WebException
                MsgBox(ex.Message)
            End Try

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    395

    Re: WebClient error

    Repped.

    Is it possible to set a limit of say 20 seconds to try connecting to the site and bail out when that is reached?

  6. #6
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: WebClient error

    It's a bit harder since the class can't do it but there is a workaround. You can either use this workaround or a similar one or switch to HttpWebRequest and HttpWebResponse classes and implement it yourself.
    This Example is far from ideal

    Code:
        WithEvents web As New WebClient
        Sub Main()
            web.DownloadStringAsync(New Uri("http://www.google.co.za"))
            System.Threading.Thread.Sleep(20000)
            If web.IsBusy Then
                web.CancelAsync()
            End If
        End Sub
    
    
        Private Sub web_DownloadStringCompleted(sender As Object, e As DownloadStringCompletedEventArgs) Handles web.DownloadStringCompleted
            Dim source As String = e.Result
        End Sub

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    395

    Re: WebClient error

    I do have some code for HttpWebRequest/HttpWebResponse code but I wouldn't know what to do. I am teetering on complete confusion with this web stuff! Any tips for checking for no response or max 20 secs using this code?

    Code:
        Public Function DownloadWebsiteSource(ByVal url As String) As String
            Dim HTTPRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
            HTTPRequest.Proxy = New WebProxy("59.148.110.112", 8909)
            Dim HTTPResponse As HttpWebResponse = DirectCast(HTTPRequest.GetResponse(), HttpWebResponse)
            Dim Response As New StreamReader(HTTPResponse.GetResponseStream())
            Dim Source As String = Response.ReadToEnd
            Response.Close()
            Return Source
        End Function

  8. #8
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: WebClient error

    I re did my code and it's a bit better now.
    Code:
        WithEvents web As New WebClient
        Sub Main()
            web.DownloadStringAsync(New Uri("http://www.google.co.za"))
            Dim I As Integer = 0
            While web.IsBusy
                I += 1
                System.Threading.Thread.Sleep(500)
                If I = 40 Then web.CancelAsync()
            End While
        End Sub
    
        Private Sub web_DownloadStringCompleted(sender As Object, e As DownloadStringCompletedEventArgs) Handles Web.DownloadStringCompleted
            Dim source As String = e.Result
        End Sub

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    395

    Re: WebClient error

    I want to do a threaded version of the downloading of the webpage sourcecode as I build up my app. I have yet to learn threading, although I saw a brief tutorial on it. The fact that you are using threading in the webclient version already, does that mean its better for me to use a HttpWebRequest version, like in my previously posted new code, but with the max 20 secs added?

  10. #10
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: WebClient error

    The webClient class is based on the Httpwebrequest and response classes. those classes are only for very specialized needs and for downloading a page's source code the WebClient is probably the best. the WebClient Eventually times out albeit not at 20 seconds.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width