小编naz*_*kus的帖子

在请求分派之前解码百分比编码的斜杠("/")

我有一个URL包含几个斜杠字符(/)作为文件名的一部分(而不是URL).但是当我发送http请求时,百分比编码%2F被转换为/请求分派之前,因此生成错误的URL.

如何在PowerShell中创建一个忽略百分比编码值的文字http请求?

使用的实际网址(Chromium浏览器):

https://www.googleapis.com/download/storage/v1/b/chromium-browser-continuous/o/Win_x64%2F292817%2Fchrome-win32.zip?generation=1409504089694000&alt=media


我试过Invoke-WebRequestcmdlet:

Invoke-WebRequest -Uri $ChromeUrl -OutFile $FilePath -Verbose

VERBOSE: GET https://www.googleapis.com/download/storage/v1/b/chromium-browser-continuous/o/Win_x64/292817/chrome-win32.zip?generation=1409504089694000&alt=media with 0-byte payload1`
Run Code Online (Sandbox Code Playgroud)

找不到错误.

还试过WebClientDownloadFile方法:

$wclient = New-Object System.Net.WebClient
$wclient.DownloadFile($ChromeUrl, $FilePath)
Run Code Online (Sandbox Code Playgroud)

由于再次请求错误的URL而返回404.


解决方法1(成功)

briantistTanuj Mathur提供的基于反射的解决方法都很有效.后者:

$UrlFixSrc = @" 
using System;
using System.Reflection;

public static class URLFix 
{ 
    public static void ForceCanonicalPathAndQuery(Uri uri)
    {
        string paq = uri.PathAndQuery;
        FieldInfo flagsFieldInfo = typeof(Uri).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
        ulong flags = (ulong) …
Run Code Online (Sandbox Code Playgroud)

.net url powershell uri

13
推荐指数
1
解决办法
2425
查看次数

标签 统计

.net ×1

powershell ×1

uri ×1

url ×1