编码参数的URL [英] Encoding parameters for a URL
问题描述
我有一个正在建设一个URL Silverlight应用程序。此URL是基于REST的服务的调用。这个服务期望表示的位置的单一参数。该位置是在城市国家的形式。要构建这个URL,我打电话下面的代码:
I have a Silverlight application that is building a URL. This URL is a call to a REST-based service. This service expects a single parameter that represents a location. The location is in the form of "city, state". To build this URL, I'm calling the following code:
string url = "http://www.domain.com/myService.svc/";
url += HttpUtility.UrlEncode(locationTextBox.Text);
如果用户输入芝加哥,伊利诺伊进locationTextBox,结果是这样的:
If a user enters "chicago, il" into locationTextBox, the result looks like this:
http://www.domain.com/myService.svc/chicago%2c+il
在现实中,虽然,我是那种期待的URL看起来像;
In reality though, I was kind of expecting the URL to look like;
http://www.domain.com/myService.svc/chicago,%20il
在通过浏览器URL测试我的服务,一个我期待的作品。然而,正在生成的URL不工作。我在做什么错了?
When testing my service via the browser URL, the one I am expecting works. However, the url that is being generated is not working. What am I doing wrong?
推荐答案
我会推荐的乌里。 EscapeDataString ,而不是使用 HttpUtility 功能。见与HttpUtility.UrlEncode 在 Server.UrlEncode讨论。
I would recommend Uri.EscapeDataString instead of using HttpUtility functions. See discussion in Server.UrlEncode vs. HttpUtility.UrlEncode.
这篇关于编码参数的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!