Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I correctly parse a URI query string into a name-value collection in C#?
    primarykey
    data
    text
    <p>I'm using .NET 4.5 and I'm trying to parse a URI query string into a <code>NameValueCollection</code>. The right way seems to be to use <code>HttpUtility.ParseQueryString(string query)</code> which takes the string obtained from <code>Uri.Query</code>and returns a <code>NameValueCollection</code>. <code>Uri.Query</code> returns a string that is escaped according to RFC 2396, and <code>HttpUtility.ParseQueryString(string query)</code> expects a string that is URL-encoded. Assuming RFC 2396 and URL-encoding are the same thing, this should work fine.</p> <p>However, <a href="http://msdn.microsoft.com/en-us/library/ms150046.aspx" rel="nofollow noreferrer">the documentation</a> for <code>ParseQueryString</code> claims that it "uses UTF8 format to parse the query string". There is also an overloaded method which takes a <code>System.Text.Encoding</code> and then uses that instead of UTF8. </p> <p>My question is: <strong>what does it mean to use UTF8 as the encoding?</strong> The input is a <code>string</code>, which by definition (in C#) is UTF-16. How is that interpreted as UTF-8? What is the difference between using UTF8 and UTF16 as the encoding in this case? My concern is that since I'm accepting arbitrary user input, there might be some security risk if I botch the encoding (i.e. the user might be able to slip through some script exploit).</p> <p>There is a previous question on this topic (<a href="https://stackoverflow.com/q/68624/1325199">How to parse a query string into a NameValueCollection in .NET</a>) but it doesn't specifically adress the encoding problem. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload