3

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=mypersonalemail15+1@gmail.com");
                var parsed = System.Web.HttpUtility.ParseQueryString(uri.Fragment);
                var email = parsed["#email"];
                // Email is: mypersonalemail15 1@gmail.com and it should be mypersonalemail15+1@gmail.com
            }
        }
    }
Fritjof Berggren
  • 3,178
  • 5
  • 35
  • 57
  • That is working as expected. If you want to get the raw value (because your caller gave you an invalid value like this one) then manually parse it from `uri.Fragment` . – mjwills Aug 13 '21 at 11:23

1 Answers1

1

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("mypersonalemail15+1@gmail.com");
var uri = new System.Uri($"callback://gmailauth/#email={urlEncodedEmail}");
var parsed = System.Web.HttpUtility.ParseQueryString(uri.Fragment);
var email = parsed["#email"];
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • The problem is that I cannot do that as I only receive the whole uri as you I show before. And in order to access the email I have to parse the uri which would put me in the same problem – Fritjof Berggren Aug 13 '21 at 11:19
  • 3
    Then whatever is sending you that URL needs fixing as it is broken. – DavidG Aug 13 '21 at 11:21
  • 2
    Another example, an email address can contain `&`, so if the email passed to you was `me&you@gmail.com`, then the parsing would give you `me` which is even more broken. – DavidG Aug 13 '21 at 11:26
  • 2
    And if you are absolutely unable to fix the root of the problem, you are going to have to manually parse the email address out of there by yourself. – DavidG Aug 13 '21 at 11:27