在PowerShell v2中,我试图将文件上传到Google云端硬盘。由于v2没有Invoke-RestMethod
cmdlet,因此我必须使用.NET类WebClient
。
$webclient = New-Object System.Net.WebClient
$webclient.Headers.Add("Authorization", "Bearer $AccessToken")
$webclient.Headers.Add("Content-Type", 'multipart/related; boundary=boundary')
$webclient.Headers.Add("Content-Length", $UploadBody.Length) # This is wrong, why?
$webclient.UploadString($URL, "POST", $UploadBody)
例外:
“ Content-Length”标头必须使用适当的属性或方法进行修改。
PS> $Error[0].Exception.ToString() System.Management.Automation.MethodInvocationException: Exception calling "UploadString" with "3" argument(s): "An exception occurred during a WebClient request." ---> System.Net.WebException: An exception occurred during a WebClient request. ---> System.ArgumentException: The 'Content-Length' header must be modified using the appropriate property or method.
没有“ Content-Type”,上传的文件将进入根文件夹,并包含我的HTML正文。
没有“ Content-Length”,上传成功,一切看起来不错。
我仍然不明白为什么“ Content-Length”标头是错误的,其目的是什么。就可能发生的故障/掉线或正确的编码而言,这是强制性的吗?