Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET中Url编码正斜杠(/)_C#_Url_Rabbitmq - Fatal编程技术网

C# 在.NET中Url编码正斜杠(/)

C# 在.NET中Url编码正斜杠(/),c#,url,rabbitmq,C#,Url,Rabbitmq,如何强制Uri或HttpWebRequest允许同时包含/和%2f的Uri,如下所示 http://localhost:55672/api/exchanges/%2f/MyExchange 我试过这个 WebRequest request = HttpWebRequest.Create("http://localhost:55672/api/exchanges/%2f/MyExchange"); …还有这个 Uri uri = new Uri("http://localhost:

如何强制
Uri
HttpWebRequest
允许同时包含
/
%2f
的Uri,如下所示

http://localhost:55672/api/exchanges/%2f/MyExchange

我试过这个

WebRequest request = 
    HttpWebRequest.Create("http://localhost:55672/api/exchanges/%2f/MyExchange");
…还有这个

Uri uri = new Uri("http://localhost:55672/api/exchanges/%2f/MyExchange", true);
WebRequest request = HttpWebRequest.Create(uri);
UriBuilder builder = new UriBuilder();
builder.Port = 55672;
builder.Path = "api/exchanges/%2f/MyExchange";
WebRequest request = HttpWebRequest.Create(builder.Uri);
…还有这个

Uri uri = new Uri("http://localhost:55672/api/exchanges/%2f/MyExchange", true);
WebRequest request = HttpWebRequest.Create(uri);
UriBuilder builder = new UriBuilder();
builder.Port = 55672;
builder.Path = "api/exchanges/%2f/MyExchange";
WebRequest request = HttpWebRequest.Create(builder.Uri);
然而,在所有这些情况下,
request.RequestUri
http://localhost:55672/api/exchanges///MyExchange
request.GetResponse()
生成404响应


仅供参考,我正在尝试使用RabbitMQ的HTTP API,并在Chrome中键入Url以生成预期的JSON结果。

使用服务器。对包含保留字符的部分进行编码。

请使用Server.UrlEncode,这是服务器端所必需的,否则备用的将是System.Uri.EscapeDataString

我们刚刚遇到了相同的问题,并且发现如果您针对的是.NET 4.0 framework,则存在此问题。如果以.NET4.5框架为目标,它就会消失。因此,如果这是您的一个选项,请切换您的目标框架,它应该可以解决您的问题。

不久前,我在与RabbitMQ管理API通信时遇到了完全相同的问题。我写了一篇关于它的博客文章,包括几个解决方案:

您可以通过对System.Uri代码进行一些令人讨厌的反射,或者通过App.config(或Web.config)中的设置来关闭该行为:



Uri.EscapeDataString产生相同的结果:
request=HttpWebRequest.Create(“http://localhost:55672/api/exchanges/“+Uri.EscapeDataString(“/”+“/MyExchange”)我还尝试了
HttpWebRequest.Create(“http://localhost:55672/api/exchanges/%252f/PrintConnector");
有趣的是,这会产生与上面相同的
request.RequestUri
,但是
request.RequestUri.LocalPath
被正确编码为
/api/exchanges/%2f/MyConnector
FWIW我为RabbitMQ管理api构建了一个.NET客户端,这可能会为您节省一些工作:也许我做错了什么,但这两种解决方案似乎都无法在VS2010或2012(使用控制台应用程序)上运行<代码>Uri=新Uri(“http://localhost:55672/api/exchanges/%2f/MyExchange"); Console.WriteLine(uri)我在RabbitMQ.Management.Client库中正是使用了该方法。看看这里:Targeting.NET4.5修复了它,即使没有uri配置设置。也许这是.NET框架中的一个bug。似乎又回到了.NET核心2.0中。