21

I want its output as uppercase. This is what I get on Server.UrlEncode("http://"):

http%3a%2f%2f

but I need:

http%3A%2F%2F

Is there built-in solution in C#?


The url encoded shall serve as signature base string (input to signature algorithm) to create digest (hash). The hash will then be verified by other system (java,php,etc), so they need to recreate the hash by signature reconstruction first.

2
  • Does the case matter for all the %escaped characters? Commented Jun 16, 2011 at 11:34
  • 15
    This matters for something like OAuth where the difference between %2f and %2F is enough to make your signature invalid - oauth.net/core/1.0/#encoding_parameters Commented Jul 6, 2013 at 0:00

5 Answers 5

34

This will uppercase all escaped characters in your string.

string url = "http://whatever.com/something";
string lower = Server.UrlEncode(url);
Regex reg = new Regex(@"%[a-f0-9]{2}");
string upper = reg.Replace(lower, m => m.Value.ToUpperInvariant());
Sign up to request clarification or add additional context in comments.

6 Comments

that's its agent-j, but without extra ")", Thanks!
thanks for this. For some reason Twitter's OAuth requires the escaped characters to be in uppercase or else it'll give you 401 unauthorized
Thanks! (BTW, the reason this is necessary for OAuth is that the entire URL ("http://" and all) is URLEncoded and then signed. The OAuth 1.0 spec requires uppercase hexadicemal for encoded characters, but the .net UrlEncode produces lowercase - the difference causes the signatures to not match.)
@SAGExSDX: I have uppercase the url that needed to create the oauth_signature. But it still give me the 401 error. If you have the C# code, can you share it me please ?
@user1457039 var lower = HttpUtility.UrlEncode(strText); var reg = new Regex(@"%[a-f0-9]{2}"); return reg.Replace(lower, m => m.Value.ToUpperInvariant()); It's the same as the above answer.
|
25
Uri.EscapeDataString("http://")

This code return

http%3A%2F%2F

3 Comments

my favorite. Why use regex when you can use this?
This the correct answer: If you had used HttpUtility.UrlEncode('http://'); to encode it would incorrectly return lowercase encode letters, which I was incorrectly using for signatures in Oauth http requests.
This doesn't escape square brackets: []
8

I encountered the same problem, I found the answer in this link:

WebUtility.UrlEncode or HttpUtility.UrlEncode

in-short you can use:

System.Net.WebUtility.UrlEncode

which encodes into uppercase hex values

Comments

3

This is very easy

Regex.Replace( encodedString, @"%[a-f\d]{2}", m => m.Value.ToUpper() )

I.e. replace all hex letter-digit combinations to upper case

Comments

0

Assuming "http" is always the first four characters then you simply split the string after "http", UrlEncode that part and then call ToUpper() on it. Then join back together with "http" as your prefix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.