This documentation is archived and is not being maintained.

HttpServerUtility.UrlEncode Method (String, TextWriter)

.NET Framework 1.1

URL encodes a string and sends the resulting output to a TextWriter output stream.

[Visual Basic]
Overloads Public Sub UrlEncode( _
   ByVal s As String, _
   ByVal output As TextWriter _
public void UrlEncode(
 string s,
 TextWriter output
public: void UrlEncode(
 String* s,
 TextWriter* output
public function UrlEncode(
   s : String,
 output : TextWriter


The text string to encode.
The TextWriter output stream containing the encoded string.


URL encoding ensures that all browsers will correctly transmit text in URL strings. Characters such as ?, &,/, and spaces may be truncated or corrupted by some browsers so those must be encoded in <A> tags or in query strings where the strings may be re-sent by a browser in a request string.


The following example encodes a string for transmission by HTTP. It encodes the string named TestString, which contains the text "This is a <Test String>.", and copies it into the string named EncodedString as "This+is+a+%3cTest+String%3e.".

[Visual Basic] 
Dim TestString As String = "This is a <Test String>."
Dim writer As New StringWriter
Server.UrlEncode(TestString, writer)
Dim EncodedString As String = writer.ToString()

String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.UrlEncode(TestString, writer);
String EncodedString = writer.ToString();

String* TestString = S"This is a <Test String>.";
StringWriter* writer = new StringWriter();
Server->UrlEncode(TestString, writer);
String* EncodedString = writer->ToString();

var testString : String = "This is a <Test String>."
var writer : StringWriter = new StringWriter()
Server.UrlEncode(testString, writer)
var encodedString : String = writer.ToString()


Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

HttpServerUtility Class | HttpServerUtility Members | System.Web Namespace | HttpServerUtility.UrlEncode Overload List