Search code examples
c#htmlurlencodeurldecode

URL Encode functions after asking for input


I have this website I am using https://www.examplewebsiteone.com/account/verify?email=testeremail%40gmail%2Ecom&password=this%26isjust%26a%26test%24

this is what is look like decoded https://www.examplewebsiteone.com/account/[email protected] &password=this&isjust&a&test$

Encoded by https://www.w3schools.com/tags/ref_urlencode.asp

My C# Code:

private const string URL = "https://www.examplewebsiteone.com/account/verify?email=testeremail%40gmail%2Ecom&password=this%26isjust%26a%26test%24";
private const string DATA = @"{""object"":{""name"":""Name""}}";

static void Main(string[] args)
{
    Class1.CreateObject();
}

I want to ask for user input (email)(password). When user enters [email protected] (email) and this&isjust&a&test$ (password) then I want c# to encode the regular text with "Windows-1252 or UTF 8 whatever" like in example above. And use the input in URL string above. Please help, I am new to C#.


Solution

  • You can use the UrlEncode() and UrlDecode() methods of Httputility which is in System.Web namespace:

    string encoded = HttpUtility.UrlEncode(Url);
    string decode = HttpUtility.UrlDecode(encoded);