gpt4 book ai didi

javascript - 在获取 URL 之前了解 URL 的性质

转载 作者:行者123 更新时间:2023-12-03 03:03:47 25 4
gpt4 key购买 nike

GET 之前是否可以知道 URL 的性质?

我有一个以 m3u 结尾的 URL,但它不是一个可以下载的简单文件。这实际上是一个广播流。正如我期望的(有限)文件,GET 方法永远不会结束。超时选项在这种情况下不起作用(正常)。

const options = {timeout: 5000};
return HTTP.call('GET', "http://av.rasset.ie/av/live/radio/junior.m3u", options);

安全的解决方案应该是在实际获取文件之前询问响应的类型。我怎样才能做到这一点?

谢谢,迈克尔。

最佳答案

我想您可以先运行 HEAD 请求(而不是 GET)并验证 header 。然后在执行GET之后你就会知道如何 react 。

不幸的是,在这种特殊情况下,HEAD 适用于第一个请求(返回重定向):

 curl -v -X HEAD http://icecast1.rte.ie/junior               http://av.rasset.ie/av/live/radio/junior.m3u
Warning: Setting custom HTTP method to HEAD with -X/--request may not work the
Warning: way you want. Consider using -I/--head instead.
* Trying 104.16.107.29...
* TCP_NODELAY set
* Connected to av.rasset.ie (104.16.107.29) port 80 (#0)
> HEAD /av/live/radio/junior.m3u HTTP/1.1
> Host: av.rasset.ie
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 302 FOUND
< Date: Mon, 13 Nov 2017 11:07:33 GMT
< Content-Type: text/html; charset=utf-8
< Connection: keep-alive
< Set-Cookie: __cfduid=d89353ae357a0452208835b3092f0fbee1510571253; expires=Tue, 13-Nov-18 11:07:33 GMT; path=/; domain=.rasset.ie; HttpOnly
< Location: http://icecast1.rte.ie/junior
< X-Server-Name: djd
< Cache-Control: max-age=0
< Accept-Ranges: bytes
< X-Varnish: 2802121867
< X-Served-By: MISS: mt-www2.rte.ie
< CF-Cache-Status: MISS
< Server: cloudflare-nginx
< CF-RAY: 3bd1449d2764410c-HAM
* no chunk, no close, no size. Assume close to signal end
<
^C

但第二次失败(可能不支持 HEAD,应该返回 405):

 curl -v -X HEAD http://icecast1.rte.ie/junior               
Warning: Setting custom HTTP method to HEAD with -X/--request may not work the
Warning: way you want. Consider using -I/--head instead.
* Trying 89.207.56.171...
* TCP_NODELAY set
* Connected to icecast1.rte.ie (89.207.56.171) port 80 (#0)
> HEAD /junior HTTP/1.1
> Host: icecast1.rte.ie
> User-Agent: curl/7.51.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 400 Bad Request
< Server: Icecast 2.4.2
< Date: Mon, 13 Nov 2017 11:07:42 GMT
< Content-Type: text/html; charset=utf-8
< Cache-Control: no-cache
< Expires: Mon, 26 Jul 1997 05:00:00 GMT
< Pragma: no-cache
<
<html><head><title>Error 400</title></head><body><b>400 - unknown request</b></body></html>
* Curl_http_done: called premature == 0
* Closing connection 0

关于javascript - 在获取 URL 之前了解 URL 的性质,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47262282/

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