|
-
Sep 21st, 2011, 10:17 AM
#1
Thread Starter
Hyperactive Member
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
-
Sep 21st, 2011, 10:36 AM
#2
Re: WebClient error
Try
Code:
Dim Web As New WebClient
Web.Proxy = New WebProxy("69.196.16.237", 62159)
-
Sep 21st, 2011, 10:53 AM
#3
Thread Starter
Hyperactive Member
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?
-
Sep 21st, 2011, 11:09 AM
#4
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
-
Sep 21st, 2011, 11:18 AM
#5
Thread Starter
Hyperactive Member
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?
-
Sep 21st, 2011, 11:23 AM
#6
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
-
Sep 21st, 2011, 11:28 AM
#7
Thread Starter
Hyperactive Member
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
-
Sep 21st, 2011, 11:31 AM
#8
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
-
Sep 21st, 2011, 11:52 AM
#9
Thread Starter
Hyperactive Member
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?
-
Sep 21st, 2011, 12:02 PM
#10
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|