0

I have a key(12345#678!9) which I need to encode on 2 applications, one C# one Java.

C#

var key = "12345#678!9";
var enc = HttpUtility.UrlEncode(key, Encoding.UTF8);
Console.WriteLine(enc);

The output here is 12345%23678!9

Java

var key = "12345#678!9";
var enc = java.net.URLEncoder.encode(key, StandardCharsets.UTF_8.toString);
System.out.println(enc)

The output here is 12345%23678%219

As you can see the two encoded values are different, the Java code is encoding the '!' as %21 where the C# code is not.

I cant change the key nor can I change the C# code.

So a solution will need to be on the Java side.

Is there a Java the equivalent of the C# method HttpUtility.UrlEncode?

Are they other options aside from java.net.URLEncoder.encode that I could use?

Thanks in advance

Alan Mulligan
  • 1,107
  • 2
  • 16
  • 35
  • See [this answer](https://stackoverflow.com/questions/4396598/whats-the-difference-between-escapeuristring-and-escapedatastring/34189188#34189188) for exhaustive explanations of the dark corners of c# and uri escaping. – jira Jun 05 '20 at 09:13
  • What is your use case actually? Encode in C# and decode in Java or vice versa? – jira Jun 05 '20 at 09:15
  • Usecase: Two different application(1 C#, 1 Java) calling to an API, both need to use the key to generate a signature. So I am encoding in both applications. – Alan Mulligan Jun 05 '20 at 09:32
  • The API should decode the key correctly whether the exclamation mark is percent encoded or not, I think. – jira Jun 05 '20 at 09:43

0 Answers0