admin管理员组

文章数量:1355679

Before you say that this isn't possible, please refer to here. All I need is to stop the redirecting of a automatic redirect url, and get the page that it is trying to redirect to. I prefer the "Position" header, but if there is another way, that would be great.

It doesn't seem like XMLhttpRequest will work, as to send a request, the url has to be on the same domain, which isn't this case. Please help!!!

Before you say that this isn't possible, please refer to here. All I need is to stop the redirecting of a automatic redirect url, and get the page that it is trying to redirect to. I prefer the "Position" header, but if there is another way, that would be great.

It doesn't seem like XMLhttpRequest will work, as to send a request, the url has to be on the same domain, which isn't this case. Please help!!!

Share Improve this question edited Mar 8, 2017 at 3:59 Dalrae asked Mar 8, 2017 at 3:42 DalraeDalrae 571 gold badge1 silver badge8 bronze badges 5
  • There is no Position header in HTTP 1.1. – Dai Commented Mar 8, 2017 at 3:53
  • Also, XMLHttpRequest will automatically follow redirects - so I'm not sure what your problem is, exactly. – Dai Commented Mar 8, 2017 at 3:54
  • @Dai How does that website potentially, "Disable redirects" (and it works, have tested a redirected url)? I would like to figure this out. Also, there IS a position header. I am not sure what exactly to call it, but the site calls it, "response header" – Dalrae Commented Mar 8, 2017 at 3:58
  • I think more info is needed. What are you trying to GET? I ask because I scrape external sites for links and GET the url, prevent redirects and force streams with live video.. – norcal johnny Commented Mar 8, 2017 at 4:03
  • I am trying to GET assetgame.roblox./asset/?id=1818. There is a redirection link that links to a download url, and hurl.it detects it if you set your settings to this: gyazo./11bd277c94c8c0ad87fc16a6baa64dbc – Dalrae Commented Mar 8, 2017 at 4:07
Add a ment  | 

1 Answer 1

Reset to default 7

It looks like hurl.it doesn't use XMLHttpRequest, instead they make their own request (server-side) which does let them choose to follow redirections or not (for example, .NET's HttpWebRequest.AllowAutoRedirect property).

Whereas in client JavaScript you have to use XMLHttpRequest and unfortunately there is no API to disable redirects.

However there is a new Web API called Fetch - supported by Chrome since 42, Edge since release 14, and partial support in Firefox since release 39.

With Fetch you can choose to not follow redirects, like so:

var url = "http://assetgame.roblox./asset/?id=1818";
var fetchOptions = {
    redirect: 'manual' // use "manual" to disable the automatic following of redirections
};
var request = new Request( url, fetchOptions );

window
    .fetch( request )
    .then( function( response ) {

        alert( response.headers.get('Location') );

    } );

This can be put more succinctly:

fetch( "http://assetgame.roblox./asset/?id=1818", { redirect: 'manual' } )
    .then( res => alert( res.headers.get('Location') ) );

UPDATE:

According to the documentation, the Location header will not be exposed to clients, unfortunately:

http://xcatliu./fetch/#atomic-http-redirect-handling
Throughout the platform, redirects (a response whose status or internal response's (if any) status is a redirect status) are not exposed to APIs. Exposing redirects might leak information not otherwise available through a cross-site scripting attack.

So only value added by the Fetch API is a more succinct request/response API, and the ability to detect redirections and choose to not follow them (unlike XMLHttpRequest which will always follow redirections), but you can't get the raw header, unfortunately.

Note that WebSockets do not expose a true TCP socket connection, so you cannot connect to arbitrary network services using WebSockets either.

本文标签: javascriptClient sideHow do I get the quotLocationquot header of any websiteStack Overflow