WebClient Timeout

StewartBW 1,145 Reputation points
2024-05-19T14:43:38.9+00:00

Hello,

I used many WebClient : OpenRead, UploadValues, DownloadString, DownloadStringAsync, DownloadFileAsync in my app.

Need to set a timeout for each one and found this code, added the Dispose sub myself, needed to check if it's fine to use it?

A class will be added and I will be using the WebClient like before, with timeout set at the init time:

Using wc As New WebClient(5000)

Is this correct?

Public Class WebClient
    Inherits Net.WebClient
    Private _TimeoutMS As Integer = 0
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal TimeoutMS As Integer)
        MyBase.New()
        _TimeoutMS = TimeoutMS
    End Sub
    Protected Overrides Sub Dispose(ByVal Disposing As Boolean)
        On Error Resume Next
        MyBase.Dispose(Disposing)
    End Sub
    Public 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
        On Error Resume Next
        Dim MyWR As Net.WebRequest = MyBase.GetWebRequest(Address)
        If _TimeoutMS <> 0 Then MyWR.Timeout = _TimeoutMS
        Return MyWR
    End Function
End Class

Thanks :) & what's your personal opinion on the maximum wise timeout value of the following methods, what to set?

WebClient : OpenRead, UploadValues, DownloadString, DownloadStringAsync, DownloadFileAsync

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,748 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,616 Reputation points Microsoft Vendor
    2024-05-20T06:38:19.4133333+00:00

    Hi @StewartBW ,

    It's generally not a good practice to use On Error Resume Next as it suppresses all errors, making it hard to debug issues. Using Try...Finally to ensure that resources are disposed of correctly, and called the base class's Dispose method in the Finally block.

    The ideal timeout value depends on several factors, including the expected response times, network conditions, and the nature of the operations.

    For small, quick operations, use shorter timeouts (e.g., 5-10 seconds).

    For large file downloads/uploads, use longer timeouts (e.g., 1-3 minutes).

    For applications where user interaction and quick feedback are critical, favor shorter timeouts and handle retries or error messages gracefully.

    The following are the timeout periods that can be referenced.

    OpenRead: If the resource is expected to respond quickly, a timeout of 10-15 seconds is reasonable.

    UploadValues: Depending on the size of the data being uploaded, a timeout of 20-30 seconds is usually sufficient.

    DownloadStringAsync,DownloadString: For small to medium-sized responses, a timeout of 10-20 seconds is typically adequate. the exact value can be adjusted based on expected response sizes and network conditions.

    DownloadFileAsync: For large files, the timeout should be longer. A range of 1-2 minutes is usually reasonable for typical file sizes.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.