Inherit WebClient

StewartBW 1,460 Reputation points
2025-04-27T20:24:14.6433333+00:00

Hello experts,

Here's a custom WebClient class allowing to set a custom timeout, I think it's not honoring the timeout correctly, but can't find where's the problem with the class, anyone can help please? :)

Am I doing the initialize and dispose correctly?

Peace.

Friend Class _WebClient
    Inherits Net.WebClient
    Private _TimeoutMS As Integer = 0
    Friend Sub New()
        MyBase.New()
    End Sub
    Friend Sub New(ByVal TimeoutMS As Integer)
        MyBase.New()
        _TimeoutMS = TimeoutMS
        CachePolicy = New Cache.HttpRequestCachePolicy(Cache.HttpRequestCacheLevel.NoCacheNoStore)
        CachePolicy = New Cache.RequestCachePolicy(Cache.RequestCacheLevel.NoCacheNoStore)
        Encoding = Encoding.ASCII
        Headers.Add("user-agent", "Mozilla/5.0 (Compatible; MSIE 11.0; Windows NT 10.0; .NET CLR 4.0.30319)")
    End Sub
    Protected Overrides Sub Dispose(ByVal Disposing As Boolean)
        MyBase.Dispose(Disposing)
    End Sub
    Private WriteOnly Property SetTimeout() As Integer
        Set(ByVal Value As Integer)
            _TimeoutMS = Value
        End Set
    End Property
    Protected Overrides Function GetWebRequest(ByVal Address As System.Uri) As Net.WebRequest
        Dim MyWR As Net.WebRequest = MyBase.GetWebRequest(Address)
        If _TimeoutMS = 0 Then
            MyWR.Timeout = 5000
        Else
            MyWR.Timeout = _TimeoutMS
        End If
        Return MyWR
    End Function
End Class
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,837 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.