Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the HtmlEncode (or HtmlDecode) function in Visual Studio (vb.net)?

I'm making an application that involves logging into a server, however, the post data needs some encoding.

Dim strEncUsername As String = Server.HtmlEncode(Me.txtUsername.Text)

However, since this isn't an asp.net application, this doesn't work. How the hay am I supposed to do this? I tried looking for an Imports thing, but no real luck.

like image 393
Austin Burk Avatar asked Dec 21 '11 23:12

Austin Burk


People also ask

What is the use of HttpUtility HtmlEncode?

Converts a string to an HTML-encoded string. Converts a string into an HTML-encoded string, and returns the output as a TextWriter stream of output.

When should I use HtmlEncode?

Any time you are trying to output data that could include untrusted html, you should use HTMLENCODE . Encodes text and merge field values for use in HTML by replacing characters that are reserved in HTML, such as the greater-than sign ( > ), with HTML entity equivalents, such as > .

What does HttpUtility HtmlDecode do?

WebUtility.HtmlDecode Method (System.Net)Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.

What is HtmlEncode asp net?

HtmlEncode is a convenient way to access the HttpUtility. HtmlEncode method at run time from an ASP.NET application. Internally, HtmlEncode uses HttpUtility. HtmlEncode to encode strings. To encode or decode values outside of a web application, use the WebUtility class.


2 Answers

In a Windows application you can also use:

    System.Net.WebUtility.HtmlEncode

providing the Framework version is 4 or above, see: [link]https://msdn.microsoft.com/en-us/library/ee388364(v=vs.110).aspx

like image 79
MyNameHere Avatar answered Sep 30 '22 03:09

MyNameHere


If you add a reference to System.Web to your project you can use the following to html encode your string

Dim strEncUsername As String = System.Web.HttpUtility.HtmlEncode(Me.txtUsername.Text)

MSDN Documentation for HttpUtility.HtmlEncode

Edit
Screenshot of intellisense showing HtmlEncode:
Screenshot of intellisense showing HtmlEncode

Screenshot of references in project:
Screenshot of references in project

Output from application:

unsafe text: <em>evil em tags within</em>
safe text: &lt;em&gt;evil em tags within&lt;/em&gt;
like image 44
davidsleeps Avatar answered Sep 30 '22 02:09

davidsleeps