1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| public class UriFactory
{
public UriFactory() { }
public UriFactory(string host) { this.Host = host; }
public UriFactory(string host, string path) { this.Host = host; this.Path = path; }
private Dictionary<string, string> parameters = null;
private string _host = string.Empty;
private string _path = string.Empty;
public string Path
{
get { return _path; }
set { _path = value; }
}
public string Host
{
get { return _host; }
set { _host = value; }
}
public void SetParam(string key, object value)
{
if (parameters == null)
parameters = new Dictionary<string, string>();
if (parameters.ContainsKey(key))
parameters[key] = value.ToString();
else
parameters.Add(key, value.ToString());
}
public string GetParam(string key)
{
string value = string.Empty;
parameters.TryGetValue(key, out value);
return value;
}
public void RemoveParam(string key)
{
parameters.Remove(key);
}
public void ClearParam()
{
parameters.Clear();
}
public override string ToString()
{
StringBuilder str = new StringBuilder(string.Format("http://{0}/{1}", Host, Path));
if (parameters.Count > 0)
{
str.Append("?");
int i = 0;
foreach (KeyValuePair<string, string> entry in parameters)
{
if (i == 0)
{
str.Append(string.Format("{0}={1}", entry.Key, entry.Value));
i++;
}
else
str.Append(string.Format("&{0}={1}", entry.Key, entry.Value));
}
}
return str.ToString();
}
} |
Partager