0

Question:

I need to create Uri which Contains all its attributes like (OriginalString, Query) as URL encoded.

What I tried:

I am able to format input string of Uri with below options:

string encodedString = inputString.Replace("'", "%27"); //example encoding

OR

System.Net.WebUtility.UrlEncode (inputString)

Tried all approaches to convert string to encoded string, like EscapeDataString/EscapeUriString etc

What I need to achieve

var uri = new Uri(encodedString);

When I create new Uri (like above) again %27 is replaced by '.

UPDATE 1: Not all fields of uri are converted into %27. OriginalString is converted in my case.

I want to pass this uri to HttpClient.

Do we have any mechanism to make Uri with encoded string.

vITs
  • 1,651
  • 12
  • 30
  • When I use `string encodedString = @"https://www.google.com/q?=How'To'Cook'Apples"; encodedString = encodedString.Replace("'", "%27"); Uri x = new Uri(encodedString); Console.WriteLine(x.AbsoluteUri); // outputs https://www.google.com/q?=How%27To%27Cook%27Apples` It seems to work fine, Unless I misunderstand the question – DekuDesu May 21 '21 at 06:46
  • Could you please provide a sample input? – Peter Csala May 21 '21 at 07:50
  • Yeah, above sample provided by @DekuDesu can be used. It is in Xamarin .NET standard project. Seems to be working in plain .NET project. I will check and update question if required. Thanks guys for replies. – vITs May 21 '21 at 08:07
  • See my update 1 – vITs May 24 '21 at 16:07

2 Answers2

0

Based on locale settings and used keyboard, iOS may use some other apostrophe-like character, for example right single quotation mark U+2019 ('). Solution was to handle that.

vITs
  • 1,651
  • 12
  • 30
-1

Give it a try with HttpUtility.UrlEncode(input string). Refer to https://secretgeek.net/uri_enconding for more details.

Rajan Patekar
  • 105
  • 1
  • 7