URL encode in C#

别说谁变了你拦得住时间么 提交于 2019-12-11 00:04:26

问题


I'm having trouble finding the correct way to URL encode a string in C#. What I want is to encode the string some string to some%20code. Using HttpUtility.URLEncode(); method encode is to some+string.


回答1:


HttpUtility.UrlEncode does the right thing here.

Encodes a URL string. The UrlEncode method can be used to encode the entire URL, including query-string values.

When it comes to spaces on the URL, a + or %20 are both correct.

Also see URL encoding the space character: + or %20?.




回答2:


If you want spaces encoded as %20 instead of +, you have to do the encoding yourself.

In URL encoding a + means space. You can also use %20, just as you can use the character code variant for any character, but the built in encoding uses the shorter variant.




回答3:


This thread includes a discussion of some of the in-built encoding options for URIs:

How do I replace all the spaces with %20 in C#



来源:https://stackoverflow.com/questions/10784454/url-encode-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!