Using Webclient in C# How to Get the Url of a Site After Being Redirected

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

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.

how to login into a website and get redirected url contents using c#

You should do a fiddler trace to see if the redirection is an HTTP redirect, or is it javascript-inititated on the returned page.

Assuming it is an HTTP redirect (a 301 or a 302), you can use HttpWebRequest/HttpWebResponse to fetch the redirected location. On HttpWebRequest, set AllowAutoRedirect=false. After the request is made, check the HttpWebResponse object for the StatusCode property. If HTTP status code is 301 or 302, check the Location header in the Headers property of the HttpWebResponse.

Getting the location from a WebClient on a HTTP 302 Redirect?

On HttpWebRequest you can set AllowAutoRedirect to false to handle the redirect yourself.

C# Get URL after a redirect x seconds after initial call

Because of the obscure remote site redirect handling it was only possible to solve this using a lower level communication using TCPClient and NetworkStream and thus it was possible to capture all necessary data.

How to get Location header with WebClient after POST request

So,it looks like you already know the solution.

All you have to do know is configure WebClient to use it.

Let's say your server side code looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication12.Controllers
{
public class HomeController : Controller
{
[HttpPost]
public ActionResult Login(LoginModel credential)
{
//code
Response.Headers.Add("IndexHeader","IndexHeaderValue");
return RedirectToAction("About");
}

public ActionResult About()
{
Response.Headers.Add("AboutHeader", "AboutHeaderValue");
return View();
}
}

public class LoginModel
{
public string username { get; set; }
public string password { get; set; }
}
}

You can create a custom WebClient like this:

using System;
using System.Collections.Specialized;
using System.Net;

namespace ConsoleApp18
{
public class NoRedirectWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var temp = base.GetWebRequest(address) as HttpWebRequest;
temp.AllowAutoRedirect = false;
return temp;
}
}

class Program
{
static void Main(string[] args)
{
MakeRequest(new WebClient());//Prints the AboutHeader
Console.WriteLine();
MakeRequest(new NoRedirectWebClient());//Prints the IndexHeader
Console.ReadLine();
}

private static void MakeRequest(WebClient webClient)
{
var loginUrl = @"http://localhost:50900/Home/Login";
NameValueCollection formData = new NameValueCollection();
formData["username"] = "batman";
formData["password"] = "1234";

webClient.UploadValues(loginUrl, "POST", formData);

string allheaders = "";
for (int i = 0; i < webClient.ResponseHeaders.Count; i++)
allheaders += Environment.NewLine + webClient.ResponseHeaders.GetKey(i) + " = " +
webClient.ResponseHeaders.Get(i);

Console.WriteLine("******"+webClient.GetType().FullName+"*******");
Console.Write(allheaders);
}
}
}

getting source code of redirected http site via c# webclient

Welcome to the wonderful world of page scraping. The short answer is "you can't do that." Not in the general case, anyway, and certainly not with WebClient. The problem appears to be that some Javascript does the redirection. And since all WebClient does is download the page, it's not even going to download the Javascript. Much less parse and execute it.

You might be able to do this by creating a program that uses the WebBrowser class. You can have it load the page. It should do the redirect and then you can inspect the result, which should be the page you were looking for. I haven't actually done this, but it does seem possible.

Your other option is to fire up your Web browser's developer tools (like IE's F12 Developer Tools) and watch what's happening. You can then inspect the Javascript that's being executed as well as the modified DOM, and see where the redirect happens.

Yes, it's tedious work. But once you figure out the redirect for one page, you can probably generate the URL for the other pages you want automatically.

webclient or httpwebrequest to retrieve hrefs and url

You can construct the url from the link and the link that you just downloaded like this:

Uri baseUri = new Uri("http://randomsite.com");
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");

Console.WriteLine(myUri.ToString()); // gives you http://randomsite.com/q?name=john&age=50

This also works if you base Url has url parameters.

As for the second question, i guess you meant that the request was redirected and you want that url instead? Then the easiest way to do so is to sub-class WebClient described here.

Uri baseUri = new Uri("http://randomsite.com");
using(var client=new WebClient())
{
var result = client.DownloadString(myUri);
//get href via HtmlAgilityPack...
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");
result = client.DownloadString(myUri);
}

C# WebClient OpenRead url

You can get the headers only using a HEAD request, like this:

var request = WebRequest.Create(sourceUri);
request.Method = "HEAD";

var response = request.GetResponse();
if (response != null) {
// You can now use response.Headers to get header info
}

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

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.



Related Topics



Leave a reply



Submit