Created
July 22, 2019 12:49
-
-
Save lofcz/55615649c7df20e8dd7a1c65c7484edb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApp4 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string data = "http://beautifulstyles.blog.cz/1305/hair-for-spring-2013/komentare/1#107434844"; | |
// 1c524ff635da12a088d22c32bac0bbb1 // správně | |
Uri uri; | |
Uri.TryCreate(data, UriKind.RelativeOrAbsolute, out uri); | |
Console.WriteLine(uri.ToString()); | |
string ch = GetCheckSum(GetUrlStandartized(uri)); | |
Console.WriteLine(ch); | |
Console.ReadLine(); | |
} | |
public static string GetCheckSum(string text) | |
{ | |
byte[] binary = Encoding.Unicode.GetBytes(text); | |
using (MD5 md5Hash = MD5.Create()) | |
{ | |
byte[] data = md5Hash.ComputeHash(binary); | |
StringBuilder sBuilder = new StringBuilder(); | |
for (int i = 0; i < data.Length; i++) | |
{ | |
sBuilder.Append(data[i].ToString("x2")); | |
} | |
return sBuilder.ToString(); | |
} | |
} | |
public static string GetUrlStandartized(Uri uri) | |
{ | |
if (uri == null) | |
throw new NullReferenceException("Url is null!"); | |
string host = uri.Host; | |
if (host.StartsWith("www.")) | |
host = host.Remove(0, 4); | |
if (host.StartsWith("ww.")) | |
host = host.Remove(0, 3); | |
string urlStandart = string.Format("http://{0}{1}{2}", host, uri.AbsolutePath, GetQueryStringStandartized(uri)); | |
return urlStandart; | |
} | |
public static string GetQueryStringStandartized(Uri uri) | |
{ | |
if (uri == null) | |
throw new NullReferenceException("Url is null!"); | |
string queryStringStandartized = string.Empty; | |
string query = uri.Query; | |
if (string.IsNullOrEmpty(query)) | |
return string.Empty; | |
if (!query.StartsWith("?")) | |
throw new Exception("Spatny QueryString! Nezacina '?'"); | |
query = query.Remove(0, 1); // odebranini '?' | |
List<string> queryStringPar = query.Split('&').ToList(); | |
if (queryStringPar != null) | |
{ | |
queryStringPar.Sort(); | |
queryStringStandartized = "?" + queryStringPar[0]; | |
queryStringPar.RemoveAt(0); | |
foreach (var item in queryStringPar) | |
{ | |
queryStringStandartized += string.Format("&{0}", item); | |
} | |
} | |
return queryStringStandartized; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment