This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
A way to figure out redirection URL
C# Issue:
If we type the following url (see warning) http://migre.me/13rQa
It will redirect to oooooo1.ru
I like to know the redirected URL from C#.
Anybody knows how to do it.
P.S: These mentioned URL could be virus affected. Please use on own risk
If migre.me has some kind of an API, maybe they can provide you with a "preview" of the site you'll be redirected to.
If they don't, you'll have to create an HttpWebRequest and see the Uri of the response.
e.g.
WebRequest request = WebRequest.Create("http://migre.me/13rQa");
var response = request.GetResponse();
With that code, the response.ResponseUri property will have the address you'll be redirected.
Hope this helps you
If you're using HttpWebRequest, set AllowAutoRedirect to false. You'll then get to see the redirect yourself. Note that this only applies to HTTP-level redirects, not meta redirects within HTML.
(I don't know offhand whether it will throw an exception, but even if it does you can examine the response to find out the status code and the data including the redirection.)
Related
EDIT:
Maybe a better tl;dr version of my question (my brain is a little mashed)...
Either (ideally): Can I detect a referral URL with an off-domain 302 redirect?
Or: Can I detect that a 302 redirect was used to access my website?
EDIT 2 (from suggestion):
Request.UrlReferrer doesn't work in this situation, it returns as simply "https://www.google.co.uk/". I was expecting perhaps the referring URL of www.google.co.uk?url=maliciouswebsite.com - but sadly not.
A malicious website appears in Google. The link in Google points to www.maliciouswebsite.com - however when you click the link, you get sent to www.mywebsite.com.
So people google "MaliciousWebsite" and find their way to MyWebsite. I did a DNS check on maliciouswebsite.com and it has been 302 Redirected to mywebsite.com.
What I'm trying to do is "If the request is a 302 redirect sent from MaliciousWebsite.com - do not show my website".
I need a way of identifying that the request originated from MaliciousWebsite in some way.
Hope that makes sense!?
I assume you are using ASP.NET since you tagged the question as C#.
Well, ASP.NET offers a property that give you the address of the page where the request to your page originated from.
Try to use
Request.UrlReferrer
http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer(v=vs.110).aspx
So I have a google shortened Url, and once I click on it and hit my controller, I want to be able to see what the original goo.gl url was before it got resolved. How on earth do I do this?
I've tried Request.UrlReferrer.AboluteUri and System.Web.HttpContext.Current.Request.Url.AbsoluteUri but neither seem to work. They all simply return the resolved absolute uri. Any help on this would be greatly appreciated.
Here's an example of the shortened URL - http://goo.gl/WSrJ6
This would then take the user (in testing at least) to localhost:81/college/events/details/23
So basically, when I hit the Details Controller, how do I get the original shortened url back?
Ok, this feels like more of a workaround but...
I don't see any way to resolve the shortened url from the goog.gl service. However, you could send a web request to goog.gl that only uses the HEAD HTTP verb using the shortened url.
Then, in the response to the HEAD request, the location header will be the original url (because it will send back a redirect request 301).
You can check out the response by using this tool. Put in your shortened url and then choose the HEAD verb before posting.
https://developers.google.com/url-shortener/v1/getting_started
You do a get request with the following url and you get a Json with the long url
https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/WSrJ6
Any normally configured browser sends the header HTTP_REFERER.
Doesn't a simple Request.UrlReferrer work? Or, something like HttpContext.Current.Request.ServerVariables["HTTP_REFERER"] or ServerVariables["HTTP_REFERER"]?
That, depending on where you are coding, but the point is to grab the header from the request.
HTTP_REFERER should always contain the previous (referer) url. I don't see why you're getting the resolved url there, unless it's a second redirection (e.g. your shortener pointed to http://server.com and your web server is configured to redirect missing www to http://www.server.com).
HTH
Francisco
In my Project i don't want to show query string values to users. For that case i used URL Rewriting in asp.net. So my URL Looks like below.
http://localhost/test/default.aspx?id=1
to
http://localhost/test/general.aspx
The first URL will be rewrites to second URL, but it will still executes the default.aspx page with that query string value. This is working fine.
But my question is that, is there any way the user can find that original URL in browser?
The answer is no.
The browser can't tell what actual script ended up servicing the request - it only knows what it sent to the server (unless the server issued a redirect, but then the browser would make a new request to the redirect target).
Since URL rewriting takes an incoming request and routes it to a different resource, I believe the answer is yes. Somewhere in your web traffic you are requesting http://localhost/test/default.aspx?id=1 and it is being rewritten as the new request http://localhost/test/general.aspx.
While this may hide the original request from displaying in the browser, at some point it did send that original URL as an HTTP GET.
As suggested, use Firebug or Fiddler to sniff the traffic.
I figured answer for my question. We can easily found the rewritten urls. If we saw the view source of that page in browser then we can see that original url with querystring values.
i want to check if a url is from youtube.com website or the mobile version of the site.
is there a robust way to do this?
checking the url contains "youtube.com" does not seem good to me.
whats the proper way to do it?
Use the Uri class to parse the URL and compare to the Host property.
Uri uri = new Uri(myURL);
return uri.Host.Equals("youtube.com", StringComparison.InvariantCultureIgnoreCase)
I don't know a foolproof way to make sure it's youtube.com coming in, but checking REFERER is really not all that solid: the page linking to you can fake its referer header any time it wants to:
http://www.stardrifter.org/refcontrol/
It'll be interesting to see how the security gurus answer this question.
-- pete
I know about the WebRequest and the WebResponse objects. The problem is that I do not really want to get the source code of the webpage, I only want to check to see if the link exists or not. The thing is, if I use the GetResponse method, it goes an pull the entire source code of the site.
I am creating a broken link checker with many links. It takes quite a while to check them all. If there a way to to get MINIMAL information from a weblink? Only enough information to see if the link is valid or broken (not the entire source code).
An answer (BESIDES USING ASYNCHRONOUS TRANSFER) would be greatly appreciated!
WebRequest request = HttpWebRequest.Create("http://www.foo.com/");
request.Method = "HEAD"; // Just get the document headers, not the data.
HEAD is similar to GET, only that instead of getting the file contents, we get just the headers.
A standard way of checking the existence of a link is to use a HEAD request, which causes the remote server to send the headers for the requested object, but not the object itself. If you thus requested an object that is not on the server, the server gives you the normal 404 response, but if it does exist, you get a 200 response and no data after the headers. This way very little uninteresting data goes over the wire.