gpt4 book ai didi

javascript - 客户端 - 如何获取任何网站的 "Location" header ?

转载 作者:行者123 更新时间:2023-11-29 21:08:54 27 4
gpt4 key购买 nike

在你说这不可能之前,请引用 here .我所需要的只是停止自动重定向 url 的重定向,并获取它试图重定向到的页面。我更喜欢“位置”标题,但如果有另一种方式,那就太好了。

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

最佳答案

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

而在客户端 JavaScript 中,您必须使用 XMLHttpRequest,不幸的是,没有用于禁用重定向的 API。

但是有一个名为Fetch 的新Web API - Chrome 自 42 起支持,Edge 自版本 14 起支持,Firefox 自版本 39 起部分支持。

使用 Fetch,您可以选择不遵循重定向,如下所示:

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') ) );

更新:

根据文档,Location header 不会暴露给客户端,不幸的是:

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 唯一增加的值(value)是更简洁的请求/响应 API,以及检测重定向并选择不遵循它们的能力(与 XMLHttpRequest 将始终遵循重定向),但不幸的是,您无法获取原始 header 。

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

关于javascript - 客户端 - 如何获取任何网站的 "Location" header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662571/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com