I am getting an error with this code:
It says runtime errors may occur when converting string to System.Net.IWebProxyCode:Dim Web As New WebClient
Web.Proxy = "69.196.16.237:62159"
What does that mean?
Thanks,
Jon
Printable View
I am getting an error with this code:
It says runtime errors may occur when converting string to System.Net.IWebProxyCode:Dim Web As New WebClient
Web.Proxy = "69.196.16.237:62159"
What does that mean?
Thanks,
Jon
Try
Code:Dim Web As New WebClient
Web.Proxy = New WebProxy("69.196.16.237", 62159)
That worked nicely thanks! My code is now:
How can I check for the download either timing out or the website refusing a connection?Code:Dim Web As New WebClient
Web.Proxy = New WebProxy("69.196.16.237", 62159)
Dim Source As String = Web.DownloadString("http://google.com")
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
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?
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
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
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
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?
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.