如何在不使用任何外部工具的情况下下载带有批处理文件的文件?

How can I download a file with batch file without using any external tools?

首先澄清一下这个问题是针对 HTTP(s) 下载的。对于 FTP 可能我会问(并回答)另一个问题。 这是 some similar questions - 但我想更精确。

除了排除外部工具外,我希望解决方案适用于尽可能广泛的 windows 机器类型(包括 XP、Win2003、Vista,它们仍然有足够大的份额)。 此外,由于 WSH 是可能的选项之一,我更喜欢不使用临时文件,所有内容都打包在一个 .bat 文件中(jscript 和 vbscript 都可以)。

有哪些可能的方法。

  1. "Pure" 使用 BITSADMIN 的批处理解决方案 - 命令行实用程序 在每台 windows 机器上都可用。这不是很方便,但是 an/the 唯一的选择 不应使用其他脚本语言。
  2. 使用 WSH - 可以使用三种方法 - WinHTTP , MSXML2.XMLHTTP ,InternetExlorer.Application - 所有这些都可以访问 ActiveX 按照我喜欢 them.WinHTTP 和 MSXML2.XMLHTTP 的顺序排列对象 它们的功能非常相似,但 WinHTTP 有一个 更多 stable.InternetExlorer.Application 的声誉实际上是 仅可通过 ActiveX 对象访问的 Internet Explorer 和 有些 UI 元素是不可避免的(是吗?)所以我将跳过这个。
  3. 使用 .NET - 可以创建包含所有内容的混合批处理文件 三个默认的 .NET 编译器(Jscript.net、VB.Net、C#) Jscript.net 没有多余的错误信息所以我更喜欢 it.If 我们忽略了一个事实,即所有代码都有一个已编译的 .exe 在一个文件中,所以根据我的说法,这符合要求:-) .NET 我们可以使用 System.Net.WebClient 或 System.Net.HttpWebRequest(WebClient 依赖它)或
    System.Web.HttpRequest ,但现在我只会 post System.Net.WebClient solution.And 更多相同的 ActiveX 对象 可通过 WSH 访问 too.So 确实有很多 使用 .Net.May 下载文件的方法是在未来我会更新 我的 answer.Anyway 只有 Webclient 是专门为下载而设计的。
  4. 使用 powershell - 与 .NET 具有相同的可能性,但功能更少 你可以在所有机器上安装的机会 meet.So 我会跳过 这个也是。

answers.All 脚本应使用 .bat/.cmd 扩展名保存,可直接用作批处理脚本。

  1. Certutuil(出于某些原因,在最新的 win10 版本中,这被识别为木马线程):

    certutil.exe -urlcache -split -f "https://download.sysinternals.com/files/PSTools.zip" pstools.zip

为了简单起见,可以使用一个宏:

set "download=certutil.exe -urlcache -split -f"
%download% "https://download.sysinternals.com/files/PSTools.zip" pstools.zip

CertUtil 命令可能会被滥用,在 windows 中默认从 internet.Available 下载文件,因为 Vista.For 需要 WinXP Server 2003 管理工具。

  1. 比特管理员 :

最简单的使用方法

bitsadmin /transfer myDownloadJob /download /priority normal http://downloadsrv/10mb.zip c:mb.zip

使用宏:

set "dnld=bitsadmin /transfer myDownloadJob /download /priority normal"
%dnld% "https://download.sysinternals.com/files/PSTools.zip" %cd%\pstools.zip

bitsDownloader.bat

call bitsDownloader.bat "https://download.sysinternals.com/files/PSTools.zip" pstools.zip
  1. winhhtpjs.bat is a command line http client that uses WinHttpRequest。它可以执行整个范围的 http (POST,DELETE,..) 请求,也可以用于下载文件(不是太大的文件)。也可以自定义 headers可以加
call winhhtpjs.bat "https://example.com/files/some.zip" -saveTo "c:\somezip.zip" 
  1. XMLHTTPDownloadJS.bat is bat file that uses MSXML2.XMLHTTP object to download a file . Does not offer so rich options as winhhtpjs.bat ,尽管仍然是一个选项。

    call XMLHTTPDownloadJS.bat "https://download.sysinternals.com/files/PSTools.zip pst2.zip" pstools.zip

  2. WebClientDownload.bat uses the .NET System.Net.WebClientclass。它创建一个小的 exe 文件以便自行编译,并且需要安装 .net 框架。由于 class 出现在非常早的 .net 版本中,因此它具有足够的向后兼容性

call webclientDownload.bat "https://download.sysinternals.com/files/PSTools.zip" pstools.zip
  1. 在 windows 10 的最新版本中,我们有 CURL 命令,尽管这不是向后兼容的选项。请注意,只有最新版本的 windows 默认安装了 CURL。

    curl "https://download.sysinternals.com/files/PSTools.zip" --output pstools.zip

Windows 上有一个实用程序(与 CMD 一起驻留),它可以是来自 CMD 的 运行(如果您有写入权限):

set url=https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg
set file=file.jpg
certutil -urlcache -split -f %url% %file%
:also certutil.exe -verifyctl -f -split %url% %file%

Powershell 中的 Cmdlet:

$url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg"
$file = "file.jpg"
$ProgressPreference = "SilentlyContinue";
Invoke-WebRequest -Uri $url -outfile $file

.PowerShell 下的网络:

$url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg"
$file = "file.jpg"
# Add the necessary .NET assembly
Add-Type -AssemblyName System.Net.Http
# Create the HttpClient object
$client = New-Object -TypeName System.Net.Http.Httpclient
$task = $client.GetAsync($url)
$task.wait();
[io.file]::WriteAllBytes($file, $task.Result.Content.ReadAsByteArrayAsync().Result)

C# 命令行构建 csc.exe:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe

using System;
using System.IO;

using System.Net.Http;
using System.Threading.Tasks;

namespace DownloadImage
{
    class Program
    {
        static async Task Main(string[] args)
        {
            using var httpClient = new HttpClient();
            var url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg";
            byte[] imageBytes = await httpClient.GetByteArrayAsync(url);

            using var fs = new FileStream("file.jpg", FileMode.Create);
            fs.Write(imageBytes, 0, imageBytes.Length);

        }
    }
}

内置 Windows 个应用程序。无需外部下载。

在 Win 10 上测试