最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to escape # symbol in url? - Stack Overflow

programmeradmin3浏览0评论

I had # symbol to pass as parameter in my URL. But it is discarding all the parameter values after #. Kindly suggest me solution. Following is my url

GetConnectiont?   
  customerID=customer1&activenode=Sv50&parent=server&frame=imHealthCheckFrame&
connectedFrom=healthCheck&username=root&password=anil#1234&fromAskUsrPwd=askPassword

Thanks in advance

I had # symbol to pass as parameter in my URL. But it is discarding all the parameter values after #. Kindly suggest me solution. Following is my url

GetConnectiont?   
  customerID=customer1&activenode=Sv50&parent=server&frame=imHealthCheckFrame&
connectedFrom=healthCheck&username=root&password=anil#1234&fromAskUsrPwd=askPassword

Thanks in advance

Share Improve this question edited Apr 4, 2014 at 12:06 ANILBABU asked Nov 5, 2013 at 11:06 ANILBABUANILBABU 3872 gold badges6 silver badges21 bronze badges 1
  • How are you reading that parameter in your Servlet? – Mark Thomas Commented Nov 5, 2013 at 23:18
Add a ment  | 

3 Answers 3

Reset to default 10

Per the answer found here: How to escape Hash character in URL

Replace # with %23.

You can find a list of all the reserved characters here: Percent-encoding.


A word on JavaScript encoding

encodeURI(str)

"Assumes that the URI is a plete URI, so does not encode reserved characters that have special meaning in the URI. encodeURI replaces all characters except the following with the appropriate UTF-8 escape sequences:" [1]

encodeURIComponent(str)

"Escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )" [2]

In your case, you would use encodeURIComponent(), only on your query string, to escape any and all reserved chracters.

!  %21 
#  %23 
$  %24 
&  %26 
'  %27 
(  %28 
)  %29 
*  %2A 
+  %2B 
,  %2C 
/  %2F 
:  %3A 
;  %3B 
=  %3D 
?  %3F 
@  %40 
[  %5B 
]  %5D

You would need to encode the parameters that may contain # and then decode back the parameter in server side code.

You can use many resources in the internet to URL-encode strings, like this. In your case, anil#1234 bees anil%231234.

You need to encode the value(if you are using string concatenation to create the url). in javascript you can use encodeURIComponent() like

encodeURIComponent('anil#1234');//or your variable here
发布评论

评论列表(0)

  1. 暂无评论