Link to home
Create AccountLog in
Avatar of cgray1223
cgray1223

asked on

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of cgray1223
cgray1223

ASKER

62c39ac38b79c392100fc39b is what I get...
>>.. but I get key=bÃÃyÃÃ

You get it where? Where are you displaying it?
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

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ÃÃ
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


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

The key looks pretty 'binary' actually - is it?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer