Using the WebClient class I can get the title of a website easily enough:
WebClient x = new WebClient();
string source = x.DownloadString(s);
string title = Regex.Match(source,
@"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>",
RegexOptions.IgnoreCase).Groups["Title"].Value;
I want to store the URL and the page title. However when following a link such as:
I'm clearly going to want to get the Url I'm redirected to.
QUESTIONS
Is there a way to do this using the WebClient
class?
How would I do it using HttpResponse
and HttpRequest
?
I know this is already an answered question, but this works pretty to me:
Cheers.! ;)
With an
HttpWebRequest
, you would set theAllowAutoRedirect
property tofalse
. When this happens, any response with a status code between 300-399 will not be automatically redirected.You can then get the new url from the response headers and then create a new
HttpWebRequest
instance to the new url.With the
WebClient
class, I doubt you can change it out-of-the-box so that it does not allow redirects. What you could do is derive a class from theWebClient
class and then override theGetWebRequest
and theGetWebResponse
methods to alter theWebRequest
/WebResponse
instances that the base implementation returns; if it is anHttpWebRequest
, then set theAllowAutoRedirect
property tofalse
. On the response, if the status code is in the range of 300-399, then issue a new request.However, I don't know that you can issue a new request from within the
GetWebRequest
/GetWebResponse
methods, so it might be better to just have a loop that executes withHttpWebRequest
/HttpWebResponse
until all the redirects are followed.In case you are only interested in the redirect URI you can use this code:
The method will return
Please note: The
using
statement (or a finalresponse.close()
) is essential. See MSDN Library for details. Otherwise you may run out of connections or get a timeout when executing this code multiple times.Ok this is really hackish, but the key is to use the HttpWebRequest and then set the AllowAutoRedirect property to true.
Here's a VERY hacked together example
enter code here
I got the Uri for the redirected page and the page contents.
The WebClient class has an option to follow redirects. Set that option and you should be fine.