WebClient stopped working after redirect

StewartBW 1,870 Reputation points
2025-07-23T07:54:31.5033333+00:00

Hello everyone,

I've added this to my .htaccess to redirect all www and non-www request to the https-with-www:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off  
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]

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

From my code, I still use DownloadStringAsync/DownloadFileAsync/UploadValues and pass the non https version: www.domain.com and expect the WebClient to do the redirection, seems download methods work, but upload won't!

So I created a new NotInheritable Class that Inherits from WebClient:

    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

And UploadValues still does not work, any idea how to fix it without changing the URL to https in code?

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.