Create new Uri using Url encoded string in C# - c#

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.

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.

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

Related

Why does System.Uri break my url?

I have the following C# code Uri uri = new Uri("http://localhost/query?param=%E2%80%AE"); and uri interprets it like http://localhost/query?param= instead of http://localhost/query?param=%E2%80%AE. As a result http web server gets http://localhost/query?param= (without value of this parameter). Why does it break my url and how can I create HttpWebRequest correctly using my http://localhost/query?param=%E2%80%AE?
P.S. I have got the %E2%80%AE using System.Uri.EscapeDataString(Convert.ToString((char)8238)).
ToString() will try to render the uri as a string. i.e., it will unescape escaped characters. However the escaped sequence %E2%80%AE is not printable.
Use the AbsoluteUri instead.
var uriStr = uri.AbsoluteUri; // "http://localhost/query?param=%E2%80%AE"
This is kind of mis-using of the methods exposed.
ToString() method, as derived from very first ancestor, Object, is a way to provide some printable form of the given object instance. It's not meant to be instance identity nor full serialization of all internal fields.
Uri class is designed as a URI parser/composer/dissector so it has various methods to merge or split given URI strings to get server name, local path etc. So if you need URI as a whole thing you have to use "Uri.AbsolutePath Property" http://msdn.microsoft.com/en-us/library/system.uri.absolutepath(v=vs.110).aspx

When ?id= string contains "&" symbol, Request.QueryString[id] doesn't return entire string

Does anyone know how to get the entire string?
Example:
var result = Request.QueryString[id];
returns "Jack" instead of "Jack & Jill" for the URL "http://website.com/test.html?=Jack&Jill
The problem is not in reading the parameter, but in constructing it. You have to change your link, or the code that creates the link.
You have to use URL escaping encoding:
http://website.com/test.html?=Jack%26Jill
URL encoding is supported in .NET (HttpUtility) and JS (global functions) as well.
& is a special character used to seperate paramaeters being passed. You need to encode your Url using ASP.NET provided functions.
Please use HttpServerUtility.UrlEncode() when assigning the url to id

Passing current page url as a parameter in url

I have a repeater.Insidea a repeater there is a button.On item Command I try
protected void rptPost_ItemCommand(object source, RepeaterCommandEventArgs e)
{
int contentID = Int32.Parse(e.CommandArgument.ToString());//Its return 1
string host = HttpContext.Current.Request.Url.ToString();//its return http://localhost:1377/Forum.aspx
//string URL = "~/Modules/Forum/PostDetails.aspx?ID=" + contentID +"&BackUrl=" + host;//Need help hear its not work
string URL = "~/Modules/Forum/PostDetails.aspx?ID=" + contentID ;//Its work
Response.Redirect(URL);
}
when I only try to pass ID its work but if I try to pass multipal url one is ID and next is Current Page Url its unable to find page.And gives page not found error.I unable to understand the problem.Passing Url as a parameter is not support or I missing some thing.Thanks.
Here is the solution:
host = HttpUtility.UrlEncode(host)
and here is why it works:
You are passing the host URL as a GET parameter. GET parameters are part of the GET URI for the redirect. URI syntax includes characters with special meanings, in particular you are using colon (:) and slash (/), which have special meaning in a URI string. In almost all cases special characters should not be used as part of your GET parameter data values.
For cases such as yours, where there is a need to include special characters, UrlEncoding was developed. UrlEncoding, or Percent-encoding, substitutes special characters in URIs with values encoded using the percent sign. These escape sequences are automatically converted back to the original special character as part of URI query processing. The HttpUtility class provides the UrlEncode method as a convenient way to escape all special characters in an input string.
Please note that it is a good idea to URL encode all data that you pass via GET parameters unless you have full control over the data being passed and you can guarantee that encoding is not necessary. This can be a security issue because attackers can include new parameters and otherwise manipulate your URI if you pass user input to a GET parameter without first URL encoding it.

HttpUtility.ParseQueryString without decoding special characters

Uri uri = new Uri(redirectionUrl);
NameValueCollection col = HttpUtility.ParseQueryString(uri.Query)
uri.Query is already decoded - so is there any way I can prevent ParseQueryString decoding it again?
Apart from that - is there another method to retrieve a name value collection from a Uri without modifying any components?
Encoding the uri.Query before passing it to ParseQueryString is the first thing that comes to my head.
UPDATE
Just checked the ParseQueryString method with Reflector: it assumes that the query string is encoded and you can't do anything with it... Bummer. So I think you need to parse it manually (there are plenty of ready-to-use algorithms on the Web).
Alternatively you could encode your query string properly (taking into account variable names and all special characters) before passing it to ParseQueryString method.
-- Pavel
I have faced the same problem. The solution is adding the second parameter - the encoding. It seams that everything works if you set UTF8 encoding.
NameValueCollection col = HttpUtility.ParseQueryString(uri.Query, Encoding.UTF8)

C# mvc2 encode url

i'm trying to encode an url with the code below;
var encodedUrl = HttpUtility.UrlEncode("http://www.example.com");
var decodedUrl = HttpUtility.UrlDecode("http%3A%2F%2Fwww%2Eexample%2Ecom%2F");
I'm working with the google webmaster tools api and this api expects an URL as shown in the decodedUrl variable above. Every single character is encoded there.
When i use the httputility encode function i get the following result;
http%3a%2f%2fwww.example.com
How can i use the encoding variable in such a way that every character in the url is encoded?
I'm pretty sure that HtmlUtility and AntiXss (another MS tool for encoding urls) aren't going to help here. A "." in a url is considered valid and so doesn't need to be encoded.
I think you're going to have to post-process your encoded string to further encode other characters that are not valid within teh google webmaster tools API.
i.e. do something like this...
var encodedUrl = HttpUtility.UrlEncode("http://www.example.com")
.Replace(".", "%2E");
... assuming that "." is the only character you're having problems with.
The period is not a reserved character in a URL, so it won't be encoded. See this question and answer for an elegant solution.

Categories