优化检测URL是否有效方法,当HEAD请求收到404响应时,使用GET请求再次尝试。
This commit is contained in:
parent
50dbbbdcf5
commit
f51a0aa21d
@ -15,7 +15,7 @@ export function initialize(): void {
|
|||||||
const lib = url.protocol === 'https:' ? https : http;
|
const lib = url.protocol === 'https:' ? https : http;
|
||||||
|
|
||||||
return await new Promise((resolve) => {
|
return await new Promise((resolve) => {
|
||||||
// 先用HEAD请求,如果遇到403,则再用GET请求再试一次(部分服务器可能禁止HEAD请求)。
|
// 先用HEAD请求,如果遇到403或404,则再用GET请求再试一次(部分服务器可能禁止HEAD请求)。
|
||||||
const requestOptions: http.RequestOptions | https.RequestOptions = {
|
const requestOptions: http.RequestOptions | https.RequestOptions = {
|
||||||
hostname: url.hostname,
|
hostname: url.hostname,
|
||||||
port: url.port || undefined,
|
port: url.port || undefined,
|
||||||
@ -35,7 +35,7 @@ export function initialize(): void {
|
|||||||
},
|
},
|
||||||
(res) => {
|
(res) => {
|
||||||
console.log('check-url-available HEAD', url.toString(), res.statusCode, res.statusMessage);
|
console.log('check-url-available HEAD', url.toString(), res.statusCode, res.statusMessage);
|
||||||
if (res.statusCode === 403) {
|
if (res.statusCode === 403 || res.statusCode === 404) {
|
||||||
headReq.destroy()
|
headReq.destroy()
|
||||||
const getReq = lib.get({
|
const getReq = lib.get({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user