从https://mws.amazonservices.com/获取以下错误消息:
<Type>Sender</Type>
<Code>SignatureDoesNotMatch</Code>
?
<Message>
The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
</Message>
Run Code Online (Sandbox Code Playgroud)
这是我用来计算请求的VB.net代码.出于安全原因,我已删除了SecretKey和AWSAccessKeyId.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sURL As String = "https://mws.amazonservices.com/"
Dim sRequest As String = ""
sRequest &= "Acknowledged=" & Server.UrlEncode("false")
sRequest &= "&Action=" & Server.UrlEncode("GetReportList")
sRequest &= "&AWSAccessKeyId=" & Server.UrlEncode("REMOVED-FOR-SECURITY")
sRequest &= "&Marketplace=" & Server.UrlEncode("REMOVED-FOR-SECURITY") …Run Code Online (Sandbox Code Playgroud) Uri类默认为RFC 2396.对于OpenID和OAuth,我需要符合RFC 3986的Uri转义.
默认情况下,URI中的任何保留字符都按照RFC 2396进行转义.如果启用了国际资源标识符或国际域名解析,则此行为会发生变化,在这种情况下,URI中的保留字符将根据RFC 3986和RFC 3987进行转义.
该文档还指出激活此IRI模式以及RFC 3986行为意味着将一个uri节元素添加到machine.config并将其添加到app/web.config文件中:
<configuration>
<uri>
<idn enabled="All" />
<iriParsing enabled="true" />
</uri>
</configuration>
Run Code Online (Sandbox Code Playgroud)
但是,无论是否存在于.config文件中,我都会获得与.NET 3.5 SP1应用程序相同的(非3986)转义行为. 我还需要做些什么Uri.EscapeDataString才能使用RFC 3986规则?(具体来说,要转义RFC中定义的保留字符)
我可以在IE中手动下载.
但是,使用以下代码
WebClient client = new WebClient();
client.DownloadFile(address, filename);
Run Code Online (Sandbox Code Playgroud)
显示例外:403禁止
怎么了?我怎样才能做到这一点?
其他
我有一个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) 在.Net 4.5之前,似乎System.Uri会对编码的斜杠进行编码,但此后已经修复.参考:https://stackoverflow.com/a/20733619/188740
我遇到了与冒号相同的问题.System.Uri仍然编码冒号.例:
var uri = new Uri("http://www.example.com/?foo=http%3A%2F%2Fwww.example.com");
var s = uri.ToString(); //http://www.example.com/?foo=http:%2F%2Fwww.example.com
Run Code Online (Sandbox Code Playgroud)
请注意System.Uri 如何%3A切换回:.这是一个错误吗?什么是最好的解决方法?
我试图使用System.Web从服务器下载文件.它确实有效,但有些链接给我带来了麻烦.链接看起来像这样:
http://cdn.somesite.com/r1KH3Z%2FaMY6kLQ9Y4nVxYtlfrcewvKO9HLTCUBjU8IBAYnA3vzE1LGrkqMrR9Nh3jTMVFZzC7mxMBeNK5uY3nx5K0MjUaegM3crVpFNGk6a6TW6NJ3hnlvFuaugE65SQ4yM5754BM%2BLagqYvwvLAhG3DKU9SGUI54UAq3dwMDU%2BMl9lUO18hJF3OtzKiQfrC/the_file.ext
Run Code Online (Sandbox Code Playgroud)
代码看起来基本上是这样的:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
WebResponse response = request.getResponse();
Run Code Online (Sandbox Code Playgroud)
getResponse()始终抛出异常(错误400错误请求).但是,我知道该链接有效,因为我可以使用Firefox下载该文件而不会出现问题.
我也尝试用Uri.UnescapeDataString(链接)解码链接,但该链接甚至不能在Firefox中工作.
其他链接以这种方式完美地工作..只是这些不起作用.
好的,我发现了一些使用wireshark的东西:
如果我使用Firefox打开链接,则发送:
&ME3@"dM*PNyAo PA:]GET /r1KH3Z%2FaMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs4QgbudzcrJivrAaOTYkEnozqmdoSCCY8yb1i22YtEAV/epd_outpost_12adb.flv HTTP/1.1
Host: cdn.somesite.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
我认为只有第一行是问题,因为WebRequest.Create(link)解码了url:
&MEz.@!dM/nP9@~P>.GET /r1KH3Z/aMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs6Mmh1EsQQ4vJVYUwtbLBDNx9AwCHlWDfzfSWIHzaaIo/epd_outpost_12adb.flv HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
Host: cdn.somesite.com
Run Code Online (Sandbox Code Playgroud)
(%2F替换为/)
我发现Uri类自动解码url:Uri uri = new Uri(link); //链接未解码Debug.WriteLine(uri.ToString()); //链接在此解码.
我怎么能阻止这个?
在此先感谢您的帮助.
我一直在尝试/使用以下命令从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周围添加单引号和双引号,但没有任何帮助. …
如何阻止System.Uri类取消编码传递给其构造函数的编码URL?请考虑以下代码: -
Uri uri = new Uri("http://foo.bar/foo%2FBar");
Console.WriteLine(uri.AbsoluteUri);
uri = new Uri("http://foo.bar/foo%2FBar", false);
Console.WriteLine(uri.AbsoluteUri);
uri = new Uri("http://foo.bar/foo%2FBar", true);
Console.WriteLine(uri.AbsoluteUri);
Run Code Online (Sandbox Code Playgroud)
在每种情况下,输出都是"http://foo.bar/foo/bar".如何确保在实例化Uri实例后,Uri.AbsoluteUri将返回"http://foo.bar/foo%2FBar"?
我试图让Uri停止编码'/'如下所述: 使用URL编码的斜杠获取URL
但是如何在PowerShell中实现同样的目标呢?
我试图遵循访问私有财产的路线并改变它的价值,但我无法让它发挥作用.
[Uri].GetProperties([System.Reflection.BindingFlags]::NonPublic) - 什么都不返回
有任何想法吗?
在本地计算机上使用ReSharper 8.2,在构建服务器上使用NUnit 2.6.3,发现在ReSharper中传递了一些测试并在NUnit中失败.在本地安装NUnit并且结果相同,因此计算机之间没有区别.我的两个同事进行了相同的测试并得到了相同的结果,所以看起来似乎没有弄乱我的电脑.
测试的简化版本:
[Test]
public void Test_UrlQueryString()
{
var urlInput = "http://www.domain.com/page-with-querystring?url=https://www.domain2.com/page%3Fp%3DPEPE";
var uri = new Uri(urlInput);
Assert.AreEqual(urlInput, uri.ToString());
}
[Test]
public void Test_Dot()
{
var urlInput = "http://www.domain.com/page-with-dot.?p=google";
var uri = new Uri(urlInput);
Assert.AreEqual(urlInput, uri.ToString());
}
Run Code Online (Sandbox Code Playgroud)
ReSharper输出全部为绿色.NUnit的输出:
Runtime Environment -
OS Version: Microsoft Windows NT 6.1.7601 Service Pack 1
CLR Version: 4.0.30319.18444 ( Net 4.5 )
ProcessModel: Default DomainUsage: Single
Execution Runtime: net-4.5
...................F.F.........
Tests run: 29, Errors: 0, Failures: 2, Inconclusive: 0, Time: 0.576769973208475 seconds
Not run: …Run Code Online (Sandbox Code Playgroud) .net ×5
c# ×5
powershell ×3
uri ×3
url ×2
urlencode ×2
webrequest ×2
asp.net ×1
bad-request ×1
escaping ×1
nunit ×1
oauth ×1
openid ×1
resharper ×1
unit-testing ×1
webclient ×1