Post: [C#] Query String Parser
09-05-2015, 03:53 AM #1
Winter
Purple God
(adsbygoogle = window.adsbygoogle || []).push({}); Just a simple alternative instead of having to add in the html agility pack, for people like me who hate using external dependencies.

    
private Dictionary<char, string> URLCharacterCodes = new Dictionary<char, string>() {
{'\\', "%5C"}, { '<', "3C" }, { '>', "3E" }, { '#', "23" },
{ '%', "25" }, { '{', "7B" }, { '}', "7D" }, { '|', "7C" },
{ '^', "5E" }, { '~', "7E" }, { '[', "5B" }, { ']', "5d" },
{ '`', "60" }, { ';', "3B" }, { '/', "2F" }, { '?', "3F" },
{ ':', "3A" }, { '@', "40" }, { '=', "3D" }, { '&', "26" },
{ '$', "24" }, { '+', "2B" }, { '"', "22" }, { ' ', "20" },
{ '\'', "27" }
};
/// <summary>
/// Parse a set of headers into a url query string
/// </summary>
/// <param name="Values"></param>
/// <returns>returns a query string</returns>
public string QueryString(NameValueCollection Values) {
string Query = string.Empty;

for (int a = 0; a < Values.Count; a++) {
string Key = string.Empty;
string Value = string.Empty;

foreach (char c in Values.GetKey(a))
if (URLCharacterCodes.ContainsKey(c))
Key += "%" + URLCharacterCodes[c];
else Key += c;
foreach (char c in Values.GetValues(a)[0])
if (URLCharacterCodes.ContainsKey(c))
Value += "%" + URLCharacterCodes[c];
else Value += c;

Query += string.Format("{0}={1}{2}", Key, Value, (a != (Values.Count - 1) ? "&" : ""));
}
return Query;
}


You can use it like so;
    
QueryString(new NameValueCollection() {
{ "q", "habbib hajif and habbob" }
});


The only 'real' practical use for this is if you setup your own web request system like I have and this might be of use to someone one day so I might as well post it since it's just a simple bootleg solution

(For anyone that cares it took 462 ticks to parse the example)
Last edited by Winter ; 09-05-2015 at 03:58 AM.

The following 3 users say thank you to Winter for this useful post:

Chen Madhala, kiwi_modz, Code
09-05-2015, 08:46 AM #2
Chen Madhala
Pokemon Trainer
Originally posted by Winter View Post
Just a simple alternative instead of having to add in the html agility pack, for people like me who hate using external dependencies.

    
private Dictionary<char, string> URLCharacterCodes = new Dictionary<char, string>() {
{'\\', "%5C"}, { '<', "3C" }, { '>', "3E" }, { '#', "23" },
{ '%', "25" }, { '{', "7B" }, { '}', "7D" }, { '|', "7C" },
{ '^', "5E" }, { '~', "7E" }, { '[', "5B" }, { ']', "5d" },
{ '`', "60" }, { ';', "3B" }, { '/', "2F" }, { '?', "3F" },
{ ':', "3A" }, { '@', "40" }, { '=', "3D" }, { '&', "26" },
{ '$', "24" }, { '+', "2B" }, { '"', "22" }, { ' ', "20" },
{ '\'', "27" }
};
/// <summary>
/// Parse a set of headers into a url query string
/// </summary>
/// <param name="Values"></param>
/// <returns>returns a query string</returns>
public string QueryString(NameValueCollection Values) {
string Query = string.Empty;

for (int a = 0; a < Values.Count; a++) {
string Key = string.Empty;
string Value = string.Empty;

foreach (char c in Values.GetKey(a))
if (URLCharacterCodes.ContainsKey(c))
Key += "%" + URLCharacterCodes[c];
else Key += c;
foreach (char c in Values.GetValues(a)[0])
if (URLCharacterCodes.ContainsKey(c))
Value += "%" + URLCharacterCodes[c];
else Value += c;

Query += string.Format("{0}={1}{2}", Key, Value, (a != (Values.Count - 1) ? "&" : ""));
}
return Query;
}


You can use it like so;
    
QueryString(new NameValueCollection() {
{ "q", "habbib hajif and habbob" }
});


The only 'real' practical use for this is if you setup your own web request system like I have and this might be of use to someone one day so I might as well post it since it's just a simple bootleg solution

(For anyone that cares it took 462 ticks to parse the example)


Not bad
I see what you did there.
09-05-2015, 11:42 AM #3
Winter
Purple God
Originally posted by Chen
Not bad
I see what you did there.


Is that you mango? I thought I saw you on Skype before stare
09-05-2015, 04:19 PM #4
Chen Madhala
Pokemon Trainer
Originally posted by Winter View Post
Is that you mango? I thought I saw you on Skype before stare


Haha, ok ?!

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo