Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C# URL Encode

Introduction to 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.

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:

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:

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:

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:

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.

Recommended Articles

This is a guide to C# URL Encode. Here we discuss the Working on URL Encode in C# and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –

  1. C# EventHandler
  2. C# FileInfo
  3. C# Thread Sleep
  4. C# Stack

The post C# URL Encode appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

C# URL Encode

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×