URL Encode Decode Mismatch in Java

Hello,

I need to put the below value in a url query string parameter and when the user clicks the link I will read that value. I'm having issues encoding and decoding it to the correct value. Below is what I've tried without success, ideas?

starting key=bÚËyÒÛ then I URLEncoder.encode("bÚËyÒÛ", "UTF-8") then I decode by URLDecoder.decode(context.getRequestParameterMap().get("key"), "UTF-8") but I get key=bÃÃyÃÃ

The url that gets created is http://localhost:8080/app/passwordResetConfirm.html?key=b%C3%9A%C3%8By%C3%92%10%0F%C3%9B
cgray1223Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Could you please post the output of the following, where 'theString' is the one you want to encode:
try {
            System.out.println(new java.math.BigInteger(1,
                    theString.getBytes("UTF-8")).toString(16));
        } catch (Exception e) {
            /* ignore */
        }

Open in new window

0
cgray1223Author Commented:
62c39ac38b79c392100fc39b is what I get...
0
CEHJCommented:
>>.. but I get key=bÃÃyÃÃ

You get it where? Where are you displaying it?
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

CEHJCommented:
Do this and tell me what you get
String theString = URLDecoder.decode(context.getRequestParameterMap().get("key"), "UTF-8");

	try {
	    System.out.println(new java.math.BigInteger(1,
			theString.getBytes("UTF-8")).toString(16));
	} catch (Exception e) {
	    e.printStackTrace();	
	}

Open in new window

0
cgray1223Author Commented:
The url i build is http://localhost:8080/test/passwordResetConfirm.html?user=ddd78@gmail.com&key=b%C3%9A%C3%8By%C3%92%10%0F%C3%9B but when I fetch the key off the request (when link is clicked) the value prior to decoding is bÃÃyÃÃ
0
cgray1223Author Commented:
62c383c29ac383c28b79c383c292100fc383c29b is the result of


String theString = URLDecoder.decode(context.getRequestParameterMap().get("key"), "UTF-8");

	try {
	    System.out.println(new java.math.BigInteger(1,
			theString.getBytes("UTF-8")).toString(16));
	} catch (Exception e) {
	    e.printStackTrace();	
	}

Open in new window

0
cgray1223Author Commented:

encode method:

   
String key = user.getPassword(); String encodedKey = ""; String decodedKey = "";
    
        			System.out.println("Key before: " + key);
        			try {
        			    encodedKey = URLEncoder.encode(key, "UTF-8");
        			} catch (UnsupportedEncodingException e) { e.printStackTrace(); }
        			
        			
        			
        			try{
        				 String encodedUrl="http://localhost:8080/dreamcatcher/preregistered/passwordResetConfirm.html?user=" + user.getUserId() +"&key=" + encodedKey;
        				
        				 JavaMailSenderImpl sender = new JavaMailSenderImpl();
    	   		   		 sender.setHost("localhost");
    	   		   		 sender.setPort(25);
    	   		   		 MimeMessage message = sender.createMimeMessage();
    	   		   		 MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
    	   		   		 helper.setFrom("dreamcatcher-no-reply@principal.com");
    	   		   		 helper.setTo(authenticationBean.getEmailPasswordReset());
    	   		   		 String messageText ="Click the below link to reset your password<br><br><a href=\"" + encodedUrl + "\" target=\"_blank\">RESET MY DREAMCATCHER PASSWORD</a>";
    	   		   		 helper.setText(messageText, true);
    	   		   		 helper.setSubject("Dreamcatcher Password Reset Instructions");
    	   		   		 sender.send(message);
    	   		   		 authenticationBean.setEmailPasswordReset(null);
    	   		   		 FacesUtils.addMessage(FacesMessage.SEVERITY_INFO, null, "Instructions have been sent to the entered E-Mail", null);
           		}
           		catch(Exception e){
           			FacesUtils.addErrorMessage(null,
           					"Invalid E-Mail Address was Entered", null);
           			authenticationBean.setEmailPasswordReset(null);
           		}

Open in new window

decode method:

   
 ExternalContext context = FacesUtils.getExternalContext();
        		String key=null;
    			try {
    				key = context.getRequestParameterMap().get("key");
    			} catch (Exception e1) {
    				FacesUtils.addErrorMessage(null,
        					"Invalid Credentials", null);

Open in new window

0
CEHJCommented:
The key looks pretty 'binary' actually - is it?
0
Mick BarryJava DeveloperCommented:
try this to encode:

URLEncoder.encode(value);

and don't decode the incoming parameter, its not necessary

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.