Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpUtility.ParseQueryString missing some characters

I'm trying to extract en email with the + special character but for some reason the ParseQueryString skips it:

namespace ParsingProblem
    {
        class Program
        {
            static void Main(string[] args)
            {        
                var uri = new System.Uri("callback://gmailauth/#[email protected]");
                var parsed = System.Web.HttpUtility.ParseQueryString(uri.Fragment);
                var email = parsed["#email"];
                // Email is: mypersonalemail15 [email protected] and it should be [email protected]
            }
        }
    }
like image 945
Fritjof Berggren Avatar asked Jun 11 '26 07:06

Fritjof Berggren


1 Answers

The + symbol in a URL is interpreted as a space character. To fix that, you need to URL encode the email address first. For example:

var urlEncodedEmail = System.Web.HttpUtility.UrlEncode("[email protected]");
var uri = new System.Uri($"callback://gmailauth/#email={urlEncodedEmail}");
var parsed = System.Web.HttpUtility.ParseQueryString(uri.Fragment);
var email = parsed["#email"];
like image 58
DavidG Avatar answered Jun 13 '26 19:06

DavidG