internal static void ParseURL(string url, out string username, out string password,
out string rawURL)
{
username = "";
password = "";
rawURL = "";
if (String.IsNullOrWhiteSpace(url))
{
throw new ArgumentException("URL is null or empty");
}
var protSplit = url.IndexOf("://");
if (protSplit < 1)
{
throw new ArgumentException(
String.Format("URL '{0}' contains invalid protocol", url));
}
string protocol = url.Substring(0, protSplit + 3);
string urlRest = url.Substring(protSplit + 3);
int usersplit = urlRest.IndexOf("@");
if (usersplit < 0) //no user name and password
{
rawURL = url;
return;
}
// else we have user (and probably pw) set
rawURL = protocol + urlRest.Substring(usersplit + 1);
var userAndPw = urlRest.Substring(0, usersplit);
var pwsplit = userAndPw.IndexOf(":");
if (pwsplit < 0) //only user provided
{
username = userAndPw;
return;
}
username = userAndPw.Substring(0, pwsplit);
password = userAndPw.Substring(pwsplit + 1);
}