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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28143160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:36:21  来源:igfitidea点击:

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

.netjscriptwshbatch-file

提问by npocmaka

First to clarify this question is aimed to HTTP(s) download .For FTP may be I'll ask (and answer) another question. Here are some similar questions- but I want to be more precise .

首先澄清这个问题是针对 HTTP(s) 下载。对于 FTP 可能是我会问(并回答)另一个问题。这里有一些类似的问题——但我想更准确一些。

Besides excluding external tools I want the solution(s) to be applicable for the widest possible types of windows machines (including XP,Win2003,Vista which still have big enough share). Also as WSHis one of the possible options I prefer no using of temp files and everything to be packed in a single .batfile (which is possible with both jscript and vbscript).

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

What are possible approaches.

什么是可能的方法。

  1. "Pure" batch solution with BITSADMIN- a command line utility available on every windows machine .It's not pretty convenient but is an/the only option where no other scripting language should be used.
  2. Using WSH - Three approaches are possible - WinHTTP, MSXML2.XMLHTTP,InternetExlorer.Application- all of them are accessible ActiveX objects in order of how I prefer them.WinHTTP and MSXML2.XMLHTTP are pretty similar in their capabilities but WinHTTP has a reputation of more stable.InternetExlorer.Application is in fact just the Internet explorer accessible through ActiveX object and some UI elements are unavoidable (are they?) so I'll skip this one.
  3. Using .NET - It's possible to create a hybrid batch file with all the three default .NET compilers (Jscript.net , VB.Net , C#) with Jscript.net there is no redundant error messages so I'll prefer it.If we ignore the fact that there's a compiled .exe all the code is in one file ,so according to me this fits in the requirements :-) .With .NET we can use System.Net.WebClient or System.Net.HttpWebRequest (the WebClient relies on it) or
    System.Web.HttpRequest , but for now I'll post only System.Net.WebClient solution.And even more same ActiveX objects accessible with WSH are available here too.So there are really many ways to dowanload a file with .Net.May be in the future I'll update my answer.Anyway only the Webclient is especially designed for download.
  4. Using powershell - has same possibilities as .NET but with less chances to be installed on all machines you can meet.So I'll skip this one too.
  1. 带有BITSADMIN 的“纯”批处理解决方案- 每台 Windows 机器上都可用的命令行实用程序。它不是很方便,但它是不应该使用其他脚本语言的唯一选项。
  2. 使用 WSH - 三种方法是可能的 - WinHTTPMSXML2.XMLHTTPInternetExlorer.Application- 所有这些都是可访问的 ActiveX 对象,按我喜欢的顺序排列。WinHTTP 和 MSXML2.XMLHTTP 的功能非常相似,但 WinHTTP 享有更稳定。InternetExlorer.Application 实际上只是可通过 ActiveX 对象访问的 Internet Explorer,并且某些 UI 元素是不可避免的(是吗?)所以我将跳过这个。
  3. 使用 .NET - 可以使用所有三个默认的 .NET 编译器(Jscript.net、VB.Net、C#)和 Jscript.net 创建一个混合批处理文件,没有多余的错误消息,所以我更喜欢它。如果我们忽略有一个编译的 .exe 所有代码都在一个文件中的事实,所以据我说这符合要求:-) .NET 我们可以使用 System.Net.WebClient 或 System.Net.HttpWebRequest(WebClient依赖于它) 或
    System.Web.HttpRequest ,但现在我将只发布 System.Net.WebClient 解决方案。这里也提供了更多可使用 WSH 访问的相同 ActiveX 对象。所以确实有很多方法可以加载文件使用 .Net。将来我会更新我的答案。无论如何,只有 Webclient 是专门为下载而设计的。
  4. 使用 powershell - 与 .NET 具有相同的可能性,但安装在您能遇到的所有机器上的机会较少。所以我也会跳过这个。

回答by npocmaka

The answers.All scripts should be saved with .bat/.cmdextensions and can be used directly as batch scripts.

答案。所有脚本都应该用.bat/.cmd扩展名保存,并且可以直接用作批处理脚本。

1) Certutuil(for some reasons in the newest win10 builds this is recognized as trojan thread ):

1) Certutuil(由于某些原因,在最新的 win10 版本中,这被认为是木马线程):

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

for easiness a macro can be used:

为方便起见,可以使用宏:

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

CertUtilcommand can be abused to download a file from internet.Available by default in windows since Vista.For WinXP Server 2003 Administration Tools are needed.

CertUtil命令可被滥用以从 Internet 下载文件。自 Vista 以来,默认情况下在 Windows 中可用。对于 WinXP Server 2003,需要管理工具。

2) Bitsadmin:

2) 比特管理员

simplest possible way to use it

最简单的使用方法

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

with macro:

带宏:

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

or with bitsDownloader.bat

或使用bitsDownloader.bat

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

3) winhhtpjs.batis a command line http client that uses WinHttpRequest.It can perform whole range of http (POST,DELETE,..) requests and can be used for downloading of files too (not too big files).Also custom headers can be added.

3) winhhtpjs.bat是一个使用WinHttpRequest的命令行 http 客户端它可以执行整个范围的 http (POST,DELETE,..) 请求,也可以用于下载文件(不是太大的文件)。也可以自定义标头可以添加。

call winhhtpjs.bat "https://example.com/files/some.zip" -saveTo "c:\somezip.zip" 

4) XMLHTTPDownloadJS.batis bat file that uses MSXML2.XMLHTTPobject to download a file . Does not offer so rich options as winhhtpjs.bat, though is still an option:

4) XMLHTTPDownloadJS.bat是使用MSXML2.XMLHTTP对象下载文件的bat文件。不提供像winhhtpjs.bat那样丰富的选项,但仍然是一个选项:

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

5) WebClientDownload.batuses the .NET System.Net.WebClientclass. It creates a small exe file in order to selfcompile itself and requires an installed a .net framework. As the class is presented in the very earlier versions of .net it is backward compatible enough

5) WebClientDownload.bat使用.NET System.Net.WebClient类。它会创建一个小的 exe 文件以进行自我编译,并且需要安装一个 .net 框架。由于该类出现在 .net 的早期版本中,因此它具有足够的向后兼容性

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

6) With the latest builds of windows 10 we have the CURLcommand ,though this is not so backward compatible option. Mind that only the newest versions of windows has CURL installed by default:

6) 在最新版本的 Windows 10 中,我们有CURL命令,尽管这不是向后兼容的选项。请注意,只有最新版本的 Windows 默认安装了 CURL:

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

回答by Zimba

There's a utility (resides with CMD) on Windows which can be run from CMD (if you have write access):

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%
echo Done.

Cmdlets in Powershell:

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

.Net under PowerShell:

PowerShell 下的 .Net:

$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# Command-line build with csc.exe:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe

使用 csc.exe 构建 C# 命令行: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);

        }
    }
}

Built in Windows applications. No need for external downloads.

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

Tested on Win 10

在 Win 10 上测试