EncodingUtil.urlEncode() encodes the special characters for correct URL String.
Sample Code:
String strURL = 'www.test.com?name=#1solutions';
strURL = EncodingUtil.urlEncode(
strURL, 'UTF-8'
);
System.debug(
'Encoded URL is ' +
strURL
);
Output: