WebClient stopped working after redirect

StewartBW 1,870 Reputation points
2025-07-23T08:06:56.5966667+00:00

Hi

I enabled redirect for my website to forward www and non-www http requests to the https version.

After that, WebClient UploadValues (WebRequestMethods.Http.Post) stopped working while DownloadStringAsync and DownloadFileAsync still works as expected!

I pass the non-https version of my website URL to WebClient and expect that WebClient do the redirection automatically, DownloadStringAsync and DownloadFileAsync will follow the redirection correctly, while UploadValues won't!

A NotInheritable Class that Inherits WebClient did not help:

Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As Net.WebRequest
    Dim MyWR As Net.HttpWebRequest = CType(MyBase.GetWebRequest(address), Net.HttpWebRequest)
    If MyWR IsNot Nothing Then
        MyWR.AllowAutoRedirect = True
        MyWR.MaximumAutomaticRedirections = 3
        If _TimeoutSec = 0 Then
            MyWR.Timeout = 30000
        Else
            MyWR.Timeout = _TimeoutSec * 1000
        End If
    End If
    Return MyWR 'CType(MyWR, WebRequest)
End Function

Any idea how to fix it?

Thanks in advance.

Windows development | Windows API - Win32
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.