WebClient stopped working after redirect
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 :)