Home How to get Location header with WebClient after POST request

# How to get Location header with WebClient after POST request

user3701
1#
user3701 Published in May 21, 2018, 5:02 pm

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 WebClient:

NameValueCollection formData = new NameValueCollection();

string response = Encoding.UTF8.GetString(responseBytes);

for (int i = 0; i < webClient.ResponseHeaders.Count; i++)
{
}


...gives as a result headers_2.txt that doesn't contain Location header (these headers are from the page that user is redirected to):

[HEADER_2] 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.

## UPDATE:

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 FAWebClient(Action<string> pOnLocationHeaderFound = null) :base()
{

}

{
{
for (int i = 0; i < this.ResponseHeaders.Count; i++)
{

{
}
}
}

}
}


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 WebClient.

 You need to login account before you can post.
Processed in 0.320581 second(s) , Gzip On .