Home  >  Article  >  Backend Development  >  C# URL Encode

C# URL Encode

王林
王林Original
2024-09-03 15:03:38929browse

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.

Working on URL Encode in C#

  • Whenever there is a need to encode the string URL, we make use of a method called URL Encode in C#.
  • The entire URL can be encoded using this method overloads, which also includes the query values of the string also.
  • The evaluation of the read-only fields defined using the keyword read-only is done at the run time.
  • If we want to decode and encode the values other than in a web application, a class called the web utility class can be used.
  • The special characters like punctuation, blank when passed to the stream of HTTP, there are chances it can be misinterpreted, but by making use of encoding, the not allowed characters in URL will be converted into character entities.
  • The encoding that is done can be reversed using URL decoding.

Examples of C# URL Encode

Different examples are mentioned below:

Example #1

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:

C# URL Encode

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.

Example #2

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:

C# URL Encode

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.

Example #3

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:

C# URL Encode

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.

Example #4

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:

C# URL Encode

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.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# KeywordsNext article:C# Keywords