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




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`?
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.*
You will want to look at the httpwebrequest class and its methods. You can feed it a URL and get back the source (page) to do with as you will. The MSDN articles that come up of you google httpwebrequest will help you. JavaScript *itself* has nothing built in for handling query string parameters. 


Code running in a (modern) browser you can use the [`URL` object](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) (which is part of the APIs provided by browsers to JS):





```
var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5"; //window.location.href
var url = new URL(url_string);
var c = url.searchParams.get("c");
console.log(c);
```









For older browsers (including Internet Explorer), you can use [this polyfill](https://github.com/ungap/url-search-params) or the code from the original version of this answer that predates `URL`:


You could access `location.search`, which would give you from the `?` character on to the end of the URL or the start of the fragment identifier (#foo), whichever comes first.


Then you can parse it with this:





```
function parse_query_string(query) {
  var vars = query.split("&");
  var query_string = {};
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    var key = decodeURIComponent(pair[0]);
    var value = decodeURIComponent(pair[1]);
    // If first entry with this name
    if (typeof query_string[key]  "undefined") {
      query_string[key] = decodeURIComponent(value);
      // If second entry with this name
    } else if (typeof query_string[key]  "string") {
      var arr = [query_string[key], decodeURIComponent(value)];
      query_string[key] = arr;
      // If third or later entry with this name
    } else {
      query_string[key].push(decodeURIComponent(value));
    }
  }
  return query_string;
}

var query_string = "a=1&b=3&c=m2-m3-m4-m5";
var parsed_qs = parse_query_string(query_string);
console.log(parsed_qs.c);
```




You can get the query string from the URL of the current page with:



```
var query = window.location.search.substring(1);
var qs = parse_query_string(query);

``` Copy Code

```
This is an example
```
  

C#Copy Code

```
public partial class Default2 : System.Web.UI.Page
{
    protected void Page\_Load(object sender, EventArgs e)
    {
        //calling your derived class
        MyClass objMyClass = new MyClass();
        objMyClass.DoSomthing();
    }
}
//Abstract Class
public abstract class MyAbstractClass
{
    public abstract void DoSomthing();
}
//derived class
public class MyClass:MyAbstractClass
{
    public MyClass()
    { }
    public override void DoSomthing()
    {
        //add your implimentation here
    }
}
```
  

  

[edit] added a small comment[/edit] Try the following:



  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  


```
import httplib
from urlparse import urlparse

def url\_exists(url):
    _, host, path, _, _, _ = urlparse(url)
    conn = httplib.HTTPConnection(host)
    conn.request('HEAD', path)
    return conn.getresponse().status < 400

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
``` hi chris, 
if you use relations api, you may set properties field in your query. in this field you should set the names of the properties which values must be retrieved from the server. the argument should be a string in the following format: "propertyname1, propertyname2, , , propertynamen" , wher… For a long time HtmlAgilityPack [didn't had the ability to directly query an attribute value](http://htmlagilitypack.codeplex.com/Thread/View.aspx?ThreadId=1720). You had to loop over the list of meta nodes. Here's one way - 



```
var doc = new HtmlDocument();
doc.LoadHtml(htmlString);

var list = doc.DocumentNode.SelectNodes("//meta"); 
foreach (var node in list)
{
    string content = node.GetAttributeValue("content", "");
}

```

But looks like in there is an [experimental xpath release](http://htmlagilitypack.codeplex.com/releases/view/46681) that will let you do that. 



```
doc.Document.SelectNodes("//meta/@content") 

```

will return a list of HtmlAttribute objects. How you do this will depend on a number of factors: since you haven't tried to explain in any detail, I'll assume you know nothing, and just give you the basics.  

  

The easiest way to do this is to read the text file into a single string, and then use the .NET framework to find the match:  


```
Dim text As String = File.ReadAllText("D:\Temp\MyFile.txt")
Dim index As Integer = text.IndexOf("hello")
If index >= 0 Then
   ' String is in file, starting at character "index"
End If
```
  

There are other ways, but that is pretty much the simplest. use promise.allsettled(). the following code works, it's simply going to skip over the rejected promise:
```javascript
function getwikiprofiles(data) {
 const profiles = data.people.map(person => {
 return getjson(wikiurl + person.name);
 });
 return promise.allsettled(profiles);
}
function generatehtml(data) {
 //console.log(data);
 data.foreach(person => {
 if (person.status  'fulfilled') {
 person = person.value;
 const section = document.createelement('section');
 peoplelist.appendchild(section);
 section.innerhtml = 
 ![](%24%7bperson.thumbnail.source%7d)
${person.title}



${person.description}


${person.extract}


;
 }
 });
``` Try these links to start.  
<http://www.tizag.com/htmlT/forms.php>  
[http://hoohoo.ncsa.illinois.edu/cgi/primer.html](/posts/bad_link/895404?url=http%3A%2F%2Fhoohoo.ncsa.illinois.edu%2Fcgi%2Fprimer.html)



You start your application by placing it's path in the action attribute of the form tag. try this and try to modify accordingly -



```
<?
//content is the array of titles which comes from the content.php page or the entire code can be put in the content.php
$content = array('title1 - This is the Homepage','title2 -Find new friends','title3 - Get your visa cleared now','title4 - bark about your wife here','title5 - Dont have kids yet','some other page');

$arr = explode('/',$_SERVER['PHP_SELF']);

$current_page = $arr[count($arr)-1];
//list your pages here
$pages  = array("1=>index.php","2=>friends.php","3=>nicks.php","4=>rent.php","5=>test.php");
for($i = 0; $i<count($pages);$i++)
{
	if($pages[$i] == trim($current_page))
		$pages_index = $i;
	else $pages_index = 6;
}
//This will echo the title
echo $content[pages_index];
?>
```

ask,if you stuck up somewhere.