Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using WebClient in C# is there a way to get the URL of a site after being redirected?

Tags:

c#

.net

regex

http

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:

http://tinyurl.com/dbysxp

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?

like image 511
Matthew Rathbone Avatar asked Mar 27 '09 16:03

Matthew Rathbone


People also ask

What is WebClient C?

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.

How do I get data from WebClient?

The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest. RegisterPrefix method. UploadString Sends a String to the resource and returns a String containing any response.

How do you send parameters data using WebClient POST request in C?

var url = "https://your-url.tld/expecting-a-post.aspx" var client = new WebClient(); var method = "POST"; // If your endpoint expects a GET then do it. var parameters = new NameValueCollection(); parameters. Add("parameter1", "Hello world"); parameters. Add("parameter2", "www.stopbyte.com"); parameters.


2 Answers

If I understand the question, it's much easier than people are saying - if you want to let WebClient do all the nuts and bolts of the request (including the redirection), but then get the actual response URI at the end, you can subclass WebClient like this:

class MyWebClient : WebClient {     Uri _responseUri;      public Uri ResponseUri     {         get { return _responseUri; }     }      protected override WebResponse GetWebResponse(WebRequest request)     {         WebResponse response = base.GetWebResponse(request);         _responseUri = response.ResponseUri;         return response;     } } 

Just use MyWebClient everywhere you would have used WebClient. After you've made whatever WebClient call you needed to do, then you can just use ResponseUri to get the actual redirected URI. You'd need to add a similar override for GetWebResponse(WebRequest request, IAsyncResult result) too, if you were using the async stuff.

like image 118
Will Dean Avatar answered Sep 29 '22 06:09

Will Dean


I know this is already an answered question, but this works pretty to me:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://tinyurl.com/dbysxp");  request.AllowAutoRedirect = false;  HttpWebResponse response = (HttpWebResponse)request.GetResponse();  string redirUrl = response.Headers["Location"];  response.Close();   //Show the redirected url  MessageBox.Show("You're being redirected to: "+redirUrl); 

Cheers.! ;)

like image 22
WhySoSerious Avatar answered Sep 29 '22 06:09

WhySoSerious