{"id":1280,"date":"2011-10-31T10:07:45","date_gmt":"2011-10-31T09:07:45","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1280"},"modified":"2015-09-08T20:37:01","modified_gmt":"2015-09-08T18:37:01","slug":"sphttputility-vs-httputility","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/sphttputility-vs-httputility\/","title":{"rendered":"SPHttpUtility vs HTTPUtility"},"content":{"rendered":"<h3>Comparison between SPHttpUtility and HTTPUtility &#8211; encoding and decoding<\/h3>\n<p>There are different ways of encoding and decoding querystring parameters, for example by using SPHttpUtility or HttpUtility.<br \/>\nWhile programming SharePoint I always tend to use the SPHttpUtility class, but I didn&#8217;t know exactly why. Until I accidentally used the HttpUtility and SPHttpUtility at the same time:\u00a0I noticed some differences.\u00a0\u00a0<\/p>\n<h3>Encode<\/h3>\n<p>SharePoint has a Utilities namespace, Microsoft.SharePoint.Utilities, which provides the SPHttpUtility class. One of the methods in this class is UrlKeyValueEncode with several overloads.<br \/>\nThe description of the method\u00a0<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">UrlKeyValueEncode(string keyOrValueToEncode)<\/pre>\n<p>is &#8216;Encodes the specified URL query string key or value&#8217;.\u00a0<\/p>\n<p>The HttpUtility class in the namespace System.Web contains a method UrlEnode, also with overloads. The description of<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">UrlEncode(string str)<\/pre>\n<p>is &#8216;Encodes a URL string&#8217;.<br \/>\nThe HttpUtility class doesn&#8217;t have a method to encode a query string key or value as UrlKeyValueEncode, but this is the best match.\u00a0\u00a0<\/p>\n<p>Let&#8217;s encode a space:<br \/>\nThe result while encoding with HttpUtility is &#8216;+&#8217;, encoding with SPHttpUtility results in a &#8216;%20&#8217;.<\/p>\n<p>One of the major differences is the casing of the encoded characters. SPHttpUtility encodes the characters uppercase, HttpUtility lowercase.<br \/>\nAnother difference can be seen in the picture below: HttpUtility doesn&#8217;t encode all characters, SPHttpUtility does:\u00a0\u00a0<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1283\" title=\"Encoding01\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding01.png\" alt=\"\" width=\"642\" height=\"219\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding01.png 803w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding01-300x102.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/a>\u00a0\u00a0<\/p>\n<h3>Decode<\/h3>\n<p>To decode characters\u00a0the SPHttpUtility class provides the method<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">UrlKeyValueDecode(string keyOrValueToDecode)<\/pre>\n<p>The couterpart of the UrlEncode method in the HttpUtility class is<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">UrlDecode(string str)<\/pre>\n<p>While encoding results in different outcome, decoding doesn&#8217;t.<br \/>\nAs can be seen in the picture of the encoded characters above, HttpUtility doesn&#8217;t encode e.g. &#8216;(&#8216;, &#8216;!&#8217;, &#8216;*&#8217;. When decoding these (unencoded) characters with SPHttpUtility the results stay &#8216;(&#8216;, &#8216;!&#8217;, &#8216;*&#8217;. Check the pictures below.<br \/>\nEncoded with HttpUtility, decoded these characters:\u00a0\u00a0<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding021.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1284\" title=\"Encoding021\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding021.png\" alt=\"\" width=\"638\" height=\"218\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding021.png 798w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding021-300x102.png 300w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/a><\/p>\n<p>SPHttpUtility encodes characters &#8216;(&#8216;, &#8216;!&#8217; and &#8216;*&#8217; as &#8216;%28&#8217;, &#8216;%21&#8217; and &#8216;%2A&#8217;. When decoding these characters with SPHttpUtility, but also with HttpUtility, the results are the same:\u00a0\u00a0<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding022.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1285\" title=\"Encoding022\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding022.png\" alt=\"\" width=\"644\" height=\"222\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding022.png 805w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/Encoding022-300x103.png 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/a>\u00a0\u00a0<\/p>\n<h3>Summary<\/h3>\n<p>Since decoding gives the same results with SPHttpUtility and HttpUtility, why bother the encoding method?\u00a0\u00a0<\/p>\n<p>Well, something inside me tells me SharePoint is using it somewhere internally. I didn&#8217;t figure out where, but why is the SPHttpUtility implemented if SharePoint easily could use HttpUtility?<br \/>\nBesides some feelings, the SPHttpUtility doesn&#8217;t use HttpUtility internally. When checking out the SPHttpUtility.UrlKeyValueEncode with ILSpy, the encoded values, uppercase(!), are listed is the readonly string array s_crgstrUrlHexValue.<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingILSpy.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1286\" title=\"EncodingILSpy\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingILSpy.png\" alt=\"\" width=\"605\" height=\"498\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingILSpy.png 605w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingILSpy-300x246.png 300w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/a>\u00a0\u00a0<\/p>\n<p>Wikipedia has a definition of percent encoding (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Percent-encoding\">http:\/\/en.wikipedia.org\/wiki\/Percent-encoding<\/a>) :<br \/>\n&#8216;Percent-encoding, also known as URL encoding, is a mechanism for encoding information in a Uniform Resource Identifier (URI) under certain circumstances&#8217;\u00a0\u00a0<\/p>\n<p>According to this article reserved characters (Reserved characters are those characters that sometimes have special meaning) must be encoded&#8230;<br \/>\nAccording to RFC 3986 reserved characters are:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingRFC3986.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1287\" title=\"EncodingRFC3986\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingRFC3986.png\" alt=\"\" width=\"388\" height=\"59\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingRFC3986.png 388w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingRFC3986-300x45.png 300w\" sizes=\"auto, (max-width: 388px) 100vw, 388px\" \/><\/a><\/p>\n<p>And encoded:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingRFC3986Encoded.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1288\" title=\"EncodingRFC3986Encoded\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingRFC3986Encoded.png\" alt=\"\" width=\"677\" height=\"91\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingRFC3986Encoded.png 677w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/11\/EncodingRFC3986Encoded-300x40.png 300w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/a>\u00a0\u00a0<\/p>\n<p>So HttpUtility doesn&#8217;t encode the first five characters, SPHttpUtility encodes all reserved characters.\u00a0\u00a0<\/p>\n<p>To be compatible with 3986 standard it seems SPHttpUtility is the best option to use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Comparison between SPHttpUtility and HTTPUtility &#8211; encoding and decoding There are different ways of encoding and decoding querystring parameters, for example by using SPHttpUtility or HttpUtility. While programming SharePoint I always tend to use the SPHttpUtility class, but I didn&#8217;t &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/sphttputility-vs-httputility\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[20,42,6],"class_list":["post-1280","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-c","tag-sharepoint-2010","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1280"}],"version-history":[{"count":16,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1280\/revisions"}],"predecessor-version":[{"id":1299,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1280\/revisions\/1299"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}