如何在System.Net.WebUtility.UrlEncode中强制使用%20而不是+ [英] How to force %20 instead of + in System.Net.WebUtility.UrlEncode

查看:31
本文介绍了如何在System.Net.WebUtility.UrlEncode中强制使用%20而不是+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在不想引用System.Web的类库程序集中对URL进行编码.该网址包含多个空格

I need to encode a URL in a class library assembly where I don't want to reference System.Web. The URL contains several spaces

https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quote where  symbol in ("YHOO","AAPL")&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

当我使用System.Net.WebUtility.UrlEncode()时,空格被替换为"+",这是行不通的.我需要将它们替换为%20

When I use System.Net.WebUtility.UrlEncode() the spaces are replaced with "+" which doesn't work. I need them to be replaced with %20

如何在不引用System.Web的情况下实现这一目标?

How can I achieve this without referencing System.Web?

推荐答案

您可以尝试

You could try Uri.EscapeUriString from System assembly, which escapes a URI string. For the string from the question it returns:

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20%20symbol%20in%20(%22YHOO%22,%22AAPL%22)&format=json&diagnostics=true&env=store%253A%252F%252Fdatatables.org%252Falltableswithkeys&callback=

这篇关于如何在System.Net.WebUtility.UrlEncode中强制使用%20而不是+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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