编码URL的参数 [英] Encoding parameters for a URL

查看:116
本文介绍了编码URL的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在构建URL的Silverlight应用程序。此URL是对基于REST的服务的调用。此服务期望表示位置的单个参数。位置是城市,州的形式。要构建此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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆