脚本未正确接收 url

Script not recieving url properly

我正在使用组合批处理和 java 脚本,我发现它使用批处理文件从网站检索 html,而我们解决的问题没有返回所需的输出,因为它出现在我在 firefox 中使用 url。

我用来拉取 html 的脚本是:

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone     *********************************************************

setlocal enableextensions disabledelayedexpansion

rem Batch file will delegate all the work to the script engine 
if not "%~1"=="" (
    cscript //E:JScript "%~dpnx0" %1
)

rem End of batch area. Ensure batch ends execution before reaching
rem javascript zone
exit /b

@end
// **** Javascript zone     *****************************************************

// Instantiate the needed component to make url queries
var http = WScript.CreateObject('MSXML2.ServerXMLHTTP.6.0');

// Retrieve the url parameter
var url = WScript.Arguments.Item(0)

// Make the request

http.open("GET", url, false);
http.send();

// If we get a OK from server (status 200), echo data to console

if (http.status === 200) WScript.StdOut.Write(http.responseText);

// All done. Exit
WScript.Quit(0);

我正在尝试提供脚本的 url 是 http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]

或者 http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian Nights"]

问题似乎是 space/+ 因为 none 的其他 url 我喂它使用的是 space 或 +

我调用脚本来拉取 html 的方式是:

call callurl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"

编辑:找到脚本来自 Open a URL without using a browser from a batch file

的原始线程

我所做的唯一更改是 Msxml2.XMLHTTP.6.0 更改为 MSXML2.ServerXMLHTTP.6.0,因为我发现由于安全原因,原始脚本无法加载站点。

只需将 space </code> 或加号 <code>+ 替换为 URL 编码的 space %20 .

例如http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian%20Nights"]

像这样调用 cscript:

cscript //E:JScript "%~dpnx0" "%~1"

我认为不需要对空格进行编码,而是对双引号(使用 %22)进行编码,尽管这可能需要解析整个命令行(%*),您可以尝试类似

setlocal enableDelayedExpansion
set "link=%*"
set "link=!link:"=%%22!"
....
 cscript //E:JScript "%~dpnx0" "%link%"

您也可以尝试使用 named arguments 并将整个命令行传递给脚本。

在这种情况下,问题是 windows 脚本宿主使用了参数中包含的双引号。

npocmaka 已显示 :对 url 中的引号进行编码。从我的角度来看,这是正确的(双引号是不安全的字符,应该进行编码)。

另一种解决方案是不将 URL 作为参数传递给脚本,而是将其存储在环境变量中,然后在 javascript 部分从变量中检索值

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************

    setlocal enableextensions disabledelayedexpansion

    rem Ensure we get a correct reference to current batch file
    call :getFullBatchReference _f0

    rem Batch file will delegate all the work to the script engine 
    if not "%~1"=="" (
        set "URL=%~1"
        cscript //nologo //E:JScript "%_f0%"
    )

    rem Ensure batch ends execution before reaching javascript zone
    exit /b %errorlevel%

:getFullBatchReference returnVar
    set "%~1=%~f0"
    goto :eof

@end
// **** Javascript zone *****************************************************
// Instantiate the needed component to make url queries
var http = WScript.CreateObject('MSXML2.ServerXMLHTTP.6.0');

// Retrieve the url parameter from environment variable
var url = WScript.CreateObject('WScript.Shell')
            .Environment('Process')
            .Item('URL');

var exitCode = 0;

    try {
        // Make the request
        http.open("GET", url, false);
        http.send();

        // If we get a OK from server (status 200), echo data to console
        if (http.status === 200) {
            WScript.StdOut.Write(http.responseText);
        } else {
            exitCode = http.status;
        };

    } catch (e) {
        // Something failed
        WScript.StdOut.Write('ERROR: ' + e.description );
        exitCode = 1;
    };

    // All done. Exit
    WScript.Quit( exitCode );

现在可以称呼为

geturl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"