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?
 
objectsCommented:
try this to encode:

URLEncoder.encode(value);

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

0
 
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
>>.. but I get key=bÃÃyÃÃ

You get it where? Where are you displaying it?
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.