Script not recieving url properly

前端 未结 3 1736
独厮守ぢ
独厮守ぢ 2021-01-19 07:31

I am using a combined batch and java script I found to retrieve the html from a web site using a batch file and one we address is not returning the desired output as it appe

相关标签:
3条回答
  • 2021-01-19 07:50

    call the cscript like that:

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

    I dont think the spaces needs to be encoded but rather the double quotes (with %22) though this could require to parse the whole command line (%*) you can try something like

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

    You can also try with named arguments and pass the whole command line to the script.

    0 讨论(0)
  • 2021-01-19 07:54

    In this case the problem is that the windows scripting host consumes the double quotes included in the arguments.

    npocmaka has shown one of the solutions: encode the quotes in the url. From my point of view it is the correct one (double quote is an unsafe character and should be encoded).

    Another solution is to not to pass the URL as an argument to the script, but to store it in a environment variable and then in the javascript part retrieve the value from the variable

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

    Now, it can be called as

    geturl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"
    
    0 讨论(0)
  • 2021-01-19 08:08

    simply replace the space or plus-sign + with a URL encoded space %20.

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

    0 讨论(0)
提交回复
热议问题