HttpUri.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace IFramework.Net.Http
  4. {
  5. public class HttpUri
  6. {
  7. public Dictionary<string, string> uriParams { get; private set; }
  8. private string uri = string.Empty;
  9. public HttpUri(string uri)
  10. {
  11. var s = uri.IndexOf('?');
  12. if (s < 0) return;
  13. string pString = uri.Substring(s + 1);
  14. var kvalues = pString.Split('&');
  15. uriParams = new Dictionary<string, string>(kvalues.Length);
  16. foreach (var value in kvalues)
  17. {
  18. var kv = value.Split('=');
  19. if (kv.Length <= 1) continue;
  20. uriParams.Add(kv[0], kv[1]);
  21. }
  22. }
  23. public HttpUri(string baseUri,Dictionary<string, string> keyValues)
  24. {
  25. this.uri = baseUri;
  26. this.uriParams = keyValues;
  27. }
  28. public string ToUriParamString()
  29. {
  30. var kvArray = uriParams.Select(x => x.Key + "=" + x.Value);
  31. return this.uri + "?" + string.Join("&", kvArray);
  32. }
  33. }
  34. }