Special Characters in URL query string

2019-04-10 09:03发布

I have a situation where the user is able to enter any characters they want in a URL query string.

Example:

http://localhost/default.aspx?ID=XXXX

http://localhost/default.aspx?ID=&XXXX

http://localhost/default.aspx?ID=#XXXX

The web page must accept the ID parameter as it is no matter what the characters are. However certain special characters such as ampersand(&) and pound(#) creates problems. How can I accept them as is?

4条回答
仙女界的扛把子
2楼-- · 2019-04-10 09:46

This:

encodeURIComponent(uri)

Where uri is the component after the ?ID=

查看更多
Evening l夕情丶
3楼-- · 2019-04-10 09:52

Use

userinput = escape(userinput)

then, in PHP:

$userinput = urldecode($_GET['id'])

or in JS:

userinput = unescape(userinput)
查看更多
狗以群分
4楼-- · 2019-04-10 09:54

Encode your URL HttpServerUtility.UrlEncode Method (String)

Edit: following your comment, you want to get query String value of ID

 String id = Request.QueryString["ID"];
查看更多
虎瘦雄心在
5楼-- · 2019-04-10 10:04

If the user is entering the query string, they must properly encode the query string first. If you are creating the query string yourself, such as from a form submission, you will need to use a URL encode method.

查看更多
登录 后发表回答