客户端 - 如何获得“位置”?任何网站的标题? [英] Client side - How do I get the "Location" header of any website?

查看:76
本文介绍了客户端 - 如何获得“位置”?任何网站的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您说不可能之前,请参阅此处。我只需要停止重定向自动重定向网址,并获取它尝试重定向到的页面。我更喜欢位置标题,但如果有另一种方式,那就太棒了。

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.

似乎XMLhttpRequest似乎不起作用,因为发送请求,url必须在同一个域上,这不是这种情况。请帮助!!!

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!!!

推荐答案

看起来像 hurl.it 不使用 XMLHttpRequest ,而是他们自己发出请求(服务器端),让他们选择是否遵循重定向(例如,.NET的 HttpWebRequest.AllowAutoRedirect property)。

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).

在客户端JavaScript中你必须使用 XMLHttpRequest 遗憾的是,没有API可以禁用重定向。

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

但是有一个名为抓取 - 自42以来Chrome支持,自14版以来支持Edge,以及自39版以来Firefox的部分支持。

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.com/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') );

    } );

这可以更简洁:

fetch( "http://assetgame.roblox.com/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.com/fetch/#atomic-http-redirect-handling

在整个平台中,重定向(状态或内部响应(如果有)状态为重定向状态的响应)不会暴露给API。公开重定向可能会泄漏信息而不是否则可通过跨站点脚本攻击获得。

http://xcatliu.com/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.

因此只有 Fetch API是一个更简洁的请求/响应API,能够检测重定向并选择不遵循它们(与 XMLHttpRequest 不同,这将是总是遵循重定向),但遗憾的是你不能得到原始标题。

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.

注意WebSockets不公开真正的TCP套接字连接,因此您无法使用WebSockets连接到任意网络服务。

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

这篇关于客户端 - 如何获得“位置”?任何网站的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆