1

Can you set a timeout for a request using

Net.WebClient()

I know that its possible with the WebRequest, however Id like to use the WebClient.

EDIT: I created a new Class called WbClnt containing this code:

Imports System.Net
Public Class WbClnt
    Inherits WebClient
    Protected Overrides Function GetWebRequest(ByVal uri As Uri) As WebRequest
        Dim w As WebRequest = MyBase.GetWebRequest(uri)
        w.Timeout = 5000
        Return w
    End Function
End Class

However, I cant call this function from my main form using WbClnt.GetWebRequest, probably because its protected.

16
  • 1
    Simple answer, yes, you have to inherit the WebClient and override GetWebRequest. What have you tried so far? Commented Feb 12, 2019 at 15:33
  • If you don't like the WebClient class as it is, you can derive a custom control from it, which will give you access to the underlying HttpWebRequest. Then you can set a Timeout using that class directly. It could be useful for other things, too. Handling Cookies, for example. Or, you could learn about HttpClient... Commented Feb 12, 2019 at 15:36
  • @Çöđěxěŕ How can I inherit the WebClient? Commented Feb 12, 2019 at 16:03
  • Create a new class and then inherit the WebClient in your new class. Now you can use your new class anywhere you would have used the WebClient or need it. Commented Feb 12, 2019 at 16:07
  • Okay, but by only Inheriting the WebClient Im still not able to set a timeout for the request, am I? Commented Feb 12, 2019 at 16:10

1 Answer 1

2

From all of the comment's above, here is a simple implementation on what I was suggesting to do.

Imports System.Net

Public Class MyPatientlyWebClient
    Inherits WebClient

#Region "Variables"
    Private ReadOnly _timeOut As Integer = 100000
#End Region

#Region "Properties"

    ''' <summary>
    ''' Determine's how to long to wait for request.
    ''' </summary>
    ''' <returns></returns>
    Public ReadOnly Property HowLongToWait
        Get
            Return _timeOut
        End Get
    End Property
#End Region

#Region "Constructors"
    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal timeOut As Integer)
        MyBase.New()
        If timeOut <= 0 Then timeOut = 100000
        _timeOut = timeOut
    End Sub

#End Region

#Region "Overrides"

    Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
        Dim w As System.Net.WebRequest = MyBase.GetWebRequest(address)
        w.Timeout = HowLongToWait
        Return w
    End Function

#End Region

End Class

TO USE

Dim myPaWebClient As New MyPatientlyWebClient(120000)
Dim str As String = myPaWebClient.DownloadString("https://www.google.com")

You can set the property if you want; need to change the ReadOnly, I just whipped something up for you real quick.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.