I need to use
WebClient to attain url that is inside response header, after succesfull POST request.
However the headers I get after executing
WebClient.UploadValues is the one from page after being redirected.
Using browser (red to highlight the header I need):
And this code that simulates same actions as in Browser with
NameValueCollection formData = new NameValueCollection();
formData["username"] = user;
formData["password"] = password;
byte responseBytes = webClient.UploadValues(loginUrl, "POST", formData);
string response = Encoding.UTF8.GetString(responseBytes);
string allheaders = "";
for (int i = 0; i < webClient.ResponseHeaders.Count; i++)
allheaders += Environment.NewLine + webClient.ResponseHeaders.GetKey(i) + " = " + webClient.ResponseHeaders.Get(i);
File.WriteAllText("headers_2.txt", "[HEADER_2] " + allheaders);
...gives as a result headers_2.txt that doesn't contain Location header (these headers are from the page that user is redirected to):
Pragma = no-cache
Cache-Control = no-store, no-cache, must-revalidate
Date = Wed, 14 Feb 2018 10:58:10 GMT
Expires = Thu, 19 Nov 1981 08:52:00 GMT
P3P = fffff
Set-Cookie = sid=ffffff; path=/,gameapi_console=0; expires=Sat, 17-Mar-2018 10:58:10 GMT; Max-Age=2678400; path=/,bptid=ffffff; path=/
Server = Apache
Vary = Accept-Encoding,User-Agent
Access-Control-Allow-Origin = *
Content-Type = text/html; charset=UTF-8
Transfer-Encoding = chunked
How to get Location header with WebClient? If I can whatsoever.
Thanks for comments.
I forgot to show what I tried.
So I created my own class that inherits from
WebClient. I was thinking that if I read the Headers just in the moment when
UploadValuesCompleted is fired - I will have the headers before being automatically redirected. Unfortunately for unknown reason the event is never fired even after I do call
webClient.UploadValues(loginUrl, "POST", formData).
class FAWebClient : WebClient
public event Action<string> OnLocationHeaderFound;
public FAWebClient(Action<string> pOnLocationHeaderFound = null) :base()
UploadValuesCompleted += OnUploadValuesCompleted;
if(pOnLocationHeaderFound != null)
this.OnLocationHeaderFound += pOnLocationHeaderFound;
protected override void OnUploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
if (this.OnLocationHeaderFound != null)
for (int i = 0; i < this.ResponseHeaders.Count; i++)
System.Diagnostics.Debug.WriteLine(this.ResponseHeaders.GetKey(i) + " = " + this.ResponseHeaders.Get(i));
if (this.ResponseHeaders.GetKey(i).ToLower() == "location")
Also please be aware I have workaround that problem using
HttpClient, simply setitng
HttpClient.AllowAutoRedirect to false:
var handler = new HttpClientHandler()
AllowAutoRedirect = false
Due to that I had to change a lot in my app logic, but it works.
However I keep this question open if anyone will give solution or answer how to achieve that with