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

查看:40
本文介绍了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.example.com/myService.svc/";
url += HttpUtility.UrlEncode(locationTextBox.Text);

如果用户在 locationTextBox 中输入chicago, il",结果如下:

If a user enters "chicago, il" into locationTextBox, the result looks like this:

http://www.example.com/myService.svc/chicago%2c+il

但实际上,我希望 URL 看起来像这样;

In reality though, I was kind of expecting the URL to look like;

http://www.example.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?

推荐答案

我推荐 Uri.EscapeDataString使用 HttpUtility 函数.请参阅 Server.UrlEncode 与 HttpUtility.UrlEncode 中的讨论.

I would recommend Uri.EscapeDataString instead of using HttpUtility functions. See discussion in Server.UrlEncode vs. HttpUtility.UrlEncode.

这篇关于URL 的编码参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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