【发布时间】:2013-08-06 17:27:04
【问题描述】:
我正在尝试使用 HttpClient 创建具有以下 Uri 的 GET 请求:
http://test.com?action=enterorder&ordersource=acme&resid=urn%3Auuid%3A0c5eea50-9116-414e-8628-14b89849808d 如您所见,resid 参数用 %3A 转义,即“:”字符。
当我在HttpClient请求中使用这个Uri时,url变成:
http://test.com?action=enterorder&ordersource=acme&resid=urn:uuid:0c5eea50-9116-414e-8628-14b89849808d 我收到来自服务器的错误,因为需要 %3A。
在发送请求时,任何人都知道如何保存转义的 Uri?似乎 HttpClient 在发送之前总是在字符串上未转义字符。 这是使用的代码:
Uri uri = new Uri("http://test.com?action=enterorder&ordersource=acme&resid=urn%3Auuid%3A0c5eea50-9116-414e-8628-14b89849808d");
using (HttpClient client = new HttpClient())
{
var resp = client.GetAsync(uri);
if (resp.Result.IsSuccessStatusCode)
{
var responseContent = resp.Result.Content;
string content = responseContent.ReadAsStringAsync().Result;
}
}
【问题讨论】:
-
您遇到什么错误?服务器没有理由在 url 中处理 "%3A" 而不是 ":"
-
它是安装在 Apache 服务器上的 Adobe Content Server。我知道这是一个奇怪的问题,但预计呼叫会收到 %3A 而不是:
-
如果使用
GetAsync的字符串重载还会出现这种情况吗?client.GetAsync(uri.OriginalString); -
@keyboardP 仍然无法正常工作 :( 感谢您的提示。
-
听起来像是服务器中的一个错误,因为“:”和“%3A”应该被相同地处理。
标签: c# escaping uri dotnet-httpclient