John Leehey
John Leehey

Reputation: 22240

C# Uri.EscapeDatastring() equivalent for Java

We've implemented an HTTP authentication connection, encoded in C# with Uri.EscapeDataString().

I'm trying to make an identical java test application that does the exact same thing as the C# version, but URLEncoder.encode(string, "UTF-8") adds additional encoding that isn't quite the same as the C# Uri.EscapeDataString() function.

What's the equivalent encoding method?

Upvotes: 9

Views: 4809

Answers (2)

John Leehey
John Leehey

Reputation: 22240

The best way I've found to do this is to use the URL.toURI().toString() combo:

String uriEncodeVariable = "https://localhost:443";
URL tempURL = new URL(uriEncodeVariable);
String uriResult = tempURL.toURI().toString();

Upvotes: 3

Michael Zilbermann
Michael Zilbermann

Reputation: 1428

I'm not very fluent in C# but it looks like you need something like URLEncoder ? Let's have a look to SO 213506 (http://stackoverflow.com/questions/213506/java-net-urlencoder-encodestring-is-deprecated-what-should-i-use-instead) ?

Upvotes: -1

Related Questions