我有一个URL包含几个斜杠字符(/)作为文件名的一部分(而不是URL).但是当我发送http请求时,百分比编码%2F被转换为/请求分派之前,因此生成错误的URL.
如何在PowerShell中创建一个忽略百分比编码值的文字http请求?
使用的实际网址(Chromium浏览器):
我试过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)
找不到错误.
还试过WebClient的DownloadFile方法:
$wclient = New-Object System.Net.WebClient
$wclient.DownloadFile($ChromeUrl, $FilePath)
Run Code Online (Sandbox Code Playgroud)
由于再次请求错误的URL而返回404.
由briantist和Tanuj 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) 假设我想从以下字符串创建一个Uri对象:
string url = @"http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com";
Uri uri = new Uri(url, UriKind.Absolute);
Run Code Online (Sandbox Code Playgroud)
预期结果将是:
http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com
Run Code Online (Sandbox Code Playgroud)
获得:
http://someserver.com/?param1=1&url=http://www.otherserver.com
Run Code Online (Sandbox Code Playgroud)
在允许创建Uri的许多相关方法中注意到相同的行为:Uri.TryCreate,UriBuilder.Uri等.
如何获得保留初始编码参数的Uri?
我一直在尝试/使用以下命令从powershell 访问带有字符的URL (它是对gitlab服务器的查询以检索调用的项目"foo/bar"):
Invoke-WebRequest https://server.com/api/v3/projects/foo%2Fbar -Verbose
Run Code Online (Sandbox Code Playgroud)
现在,奇怪的是使用PowerShell ISE或Visual Studio,请求是正常的.使用PowerShell本身时,URL会自动取消转义,并且请求失败.例如
在ISE/VS中:
$> Invoke-WebRequest https://server.com/api/v3/projects/foo%2Fbar -Verbose
VERBOSE: GET https://server.com/api/v3/projects/foo%2Fbar with 0-byte payload
VERBOSE: received 19903-byte response of content type application/json
StatusCode : 200
StatusDescription : OK
Content : .... data ....
Run Code Online (Sandbox Code Playgroud)
在Powershell中:
$> Invoke-WebRequest https://server.com/api/v3/projects/foo%2Fbar -Verbose
VERBOSE: GET https://server.com/api/v3/projects/foo/bar with 0-byte payload
Invoke-WebRequest : {"error":"404 Not Found"}
At line:1 char:1
+ Invoke-WebRequest 'https://server.com/api/v3/projects/foo%2Fbar ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Run Code Online (Sandbox Code Playgroud)
我尝试在URL周围添加单引号和双引号,但没有任何帮助. …
我试图让Uri停止编码'/'如下所述: 使用URL编码的斜杠获取URL
但是如何在PowerShell中实现同样的目标呢?
我试图遵循访问私有财产的路线并改变它的价值,但我无法让它发挥作用.
[Uri].GetProperties([System.Reflection.BindingFlags]::NonPublic) - 什么都不返回
有任何想法吗?
这是我的链接:
58.87.64.22/?{{%25}}蛋糕\=1
当我在 Chrome 中打开它时,它会给我正确的 400 状态代码。
但是当我使用HTTP客户端打开它时,它会返回200状态代码:
HttpClient client = new HttpClient();
var w = await client.GetAsync("http://58.87.64.22/?{{%25}}cake\\=1");
Run Code Online (Sandbox Code Playgroud)
看来 HTTP 客户端会将 URL 编码为:
58.87.64.22/?%7B%7B%25%7D%7D蛋糕%5C=1
当我在 Chrome 中测试编码的 URL 时,它将打开一个网页并返回 200 状态代码。
遇到这样的事情怎么解决呢?向此 URL 发送请求时,我需要获取 400 状态代码。
powershell ×3
url ×3
.net ×2
c# ×2
.net-6.0 ×1
escaping ×1
httpclient ×1
uri ×1
urlencode ×1
webrequest ×1