编码URL的参数 [英] Encoding parameters for a URL
问题描述
string url =http://www.domain.com /myService.svc/;
url + = HttpUtility.UrlEncode(locationTextBox.Text);
如果用户将chicago,il输入到locationTextBox,结果如下所示:
http://www.domain.com/myService.svc/chicago%2c+il
在现实中,我很期望URL看起来像;
http://www.domain.com/myService.svc/chicago,%20il
当我通过浏览器URL测试我的服务时,我期待的一切工作。但是,正在生成的URL不起作用。我做错了什么?
我会推荐 Uri 。 EscapeDataString ,而不是使用 HttpUtility 功能。请参阅 Server.UrlEncode与HttpUtility.UrlEncode 中的讨论。
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);
If a user enters "chicago, il" into locationTextBox, the result looks like this:
http://www.domain.com/myService.svc/chicago%2c+il
In reality though, I was kind of expecting the URL to look like;
http://www.domain.com/myService.svc/chicago,%20il
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?
I would recommend Uri.EscapeDataString instead of using HttpUtility functions. See discussion in Server.UrlEncode vs. HttpUtility.UrlEncode.
这篇关于编码URL的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!