Home > Article > Backend Development > C# URL Encode
The string URL can be encoded using a method called URL encoding method in C#. The entire URL can be encoded using this method overloads which includes the query values of string also, and if we want to decode and encode the values other than in web application, a class called web utility class can be used and the special characters like punctuation, blanks which when passed to the stream of HTTP. There are chances it can be misinterpreted, but by using encoding, the not allowed characters in the URL will be converted into character entities. The encoding that is done can be reversed using URL decoding.
The syntax for URL Encode in C# is as follows:
public static string UrlEncode (string strname, System.Text.Encoding e);
where strname is the text that must be encoded and
e is the encoding object used to specify the scheme of encoding.
Different examples are mentioned below:
C# program to demonstrate URL Encode to encode the given URL:
Code:
using System.IO; using System; using System.Web; //a class called program is defined public class Program { //main method is called within which a string variable is defined to store the string public static void Main() { //a variable called stringname is defined to store the URL which is to be encoded string stringname = "https://www.educba.com/"; //UrlEncode method is used to encode the given URL Console.WriteLine("The URL after encoding the given URL is: " + System.Web.HttpUtility.UrlEncode(stringname)); } }
Output:
In the above program, a class called program is defined. Then the main method is called, within which a string variable is defined to store the string. Then a variable called stringname is defined to store the URL which is to be encoded. For example, the URL that is to be encoded in this program is https://www.educba.com/. Then the UrlEncode method is used to encode the given URL. Finally, the output of the program is shown in the snapshot above.
C# program to demonstrate URL Encode to encode the given URL.
Code:
using System.IO; using System; using System.Web; //a class called program is defined public class Program { //main method is called within which a string variable is defined to store the string public static void Main() { //a variable called stringname is defined to store the URL which is to be encoded string stringname = "https://www.facebook.com/"; //UrlEncode method is used to encode the given URL Console.WriteLine("The URL after encoding the given URL is: " + System.Web.HttpUtility.UrlEncode(stringname)); } }
Output:
In the above program, a class called program is defined. Then the main method is called, within which a string variable is defined to store the string. Then a variable called stringname is defined to store the URL which is to be encoded. For example, the URL that is to be encoded in this program is https://www.facebook.com/. Then the UrlEncode method is used to encode the given URL. Finally, the output of the program is shown in the snapshot above.
C# program to demonstrate URL Encode to encode the given URL.
Code:
using System.IO; using System; using System.Web; //a class called program is defined public class Program { //main method is called within which a string variable is defined to store the string public static void Main() { //a variable called stringname is defined to store the URL which is to be encoded string stringname = "https://www.hotstar.com/in"; //UrlEncode method is used to encode the given URL Console.WriteLine("The URL after encoding the given URL is: " + System.Web.HttpUtility.UrlEncode(stringname)); } }
Output:
In the above program, a class called program is defined. Then the main method is called, within which a string variable is defined to store the string. Then a variable called stringname is defined to store the URL which is to be encoded. For example, the URL that is to be encoded in this program is https://www.hotstar.com/in. Then the UrlEncode method is used to encode the given URL. Finally, the output of the program is shown in the snapshot above.
C# program to demonstrate URL Encode to encode the given URL.
Code:
using System.IO; using System; using System.Web; //a class called program is defined public class Program { //main method is called within which a string variable is defined to store the string public static void Main() { //a variable called stringname is defined to store the URL which is to be encoded string stringname = " https://mail.google.com/mail/u/0/"; //UrlEncode method is used to encode the given URL Console.WriteLine("The URL after encoding the given URL is: " + System.Web.HttpUtility.UrlEncode(stringname)); } }
Output:
In the above program, a class called program is defined. Then the main method is called, within which a string variable is defined to store the string. Then a variable called stringname is defined to store the URL which is to be encoded. For example, the URL that is to be encoded in this program is https://mail.google.com/mail/u/0/. Then the UrlEncode method is used to encode the given URL. Finally, the output of the program is shown in the snapshot above.
In this tutorial, we understand the concept of URL Encode in C# through definition, the syntax of URL Encode, and the working of URL Encode in C# through programming examples and their outputs.
The above is the detailed content of C# URL Encode. For more information, please follow other related articles on the PHP Chinese website!