Page URL is "//host:port/abc/testr-email.html?ans1=NÃO&ans2=NÃO&ans3=NÃO&ans4=SIM&ans5=NÃO&cntr=1&ts=5&pname=TAAA
"
after using the URLEncoder.encode(String,"UTF-8"); on the page path became as below with the special characters escaped/encoded.
pagepath = "//host:port/abc/testr-email.html?ans1=N%C3%83O&ans2=N%C3%83O&ans3=N%C3%83O&ans4=SIM&ans5=N%C3%83O&counter=1&totalquestions=5&participantname=TAAA"
and below is my code snippet where i am trying to read the page content is having some special character values as "NÃO" which are being lost its encoded feature after the bufferreader process to decode it back and get the value to the sent it a email body.
Code Snippet:
url = new URL(pagepath);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = br.readLine()) != null) {
byte[] byte1 = inputLine.getBytes("UTF-8");
String sample=new String(byte1, "UTF-8");
log.info("sample Content ==>" + StringEscapeUtils.unescapeHtml4(sample));
content.append(sample);
Where the content is an a StringBuilder as below
StringBuilder content = new StringBuilder();
and the Content after the BufferedReader has lost the URLEncoded feature to convert back my special character to Decode and use it into Email.
String tempstring = StringEscapeUtils.unescapeHtml4(content.toString());
email.setHtmlMsg(tempstring);
I am observing that the email.setHtmlMsg(...). Doesn't have my "NÃO" special character and it has escaped character as "N%C3%83O".
How to get back the special character and send it to HTML message body in the send email feature. ?
Use the the URLDecoder
to have the special character from encoded string. Try the below statement.
URLDecoder.decode("N%C3%83O", "UTF-8")