Posted By

krisdb on 03/30/10


Tagged

c-sharp


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

hinesnetwork
khouser
stakmur


Remote WebRequest Get/Post


 / Published in: C#
 

  1. RemotePost req = new RemotePost(postUrl);
  2. req.Timeout = 3;
  3.  
  4. req.Add("FirstName", "First");
  5. req.Add("LastName", "Last");
  6.  
  7. string response = req.Post();
  8. string response = req.Get();
  9.  
  10. ---------------------------------------------------------------
  11.  
  12. public class RemotePost
  13. {
  14. private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection();
  15. private string Url = string.Empty;
  16.  
  17. /// <summary>
  18. /// number of seconds for timeout
  19. /// </summary>
  20. public int Timeout = 0;
  21.  
  22. /// <summary>
  23. /// Url to post to
  24. /// </summary>
  25. public RemotePost(string url)
  26. {
  27. Url = url;
  28. }
  29.  
  30. /// <summary>
  31. /// Parameters to add to the post
  32. /// </summary>
  33. /// <param name="name">parameter name</param>
  34. /// <param name="value">parameter value</param>
  35. public void Add(string name, string value)
  36. {
  37. Inputs.Add(name, value);
  38. }
  39.  
  40. /// <summary>
  41. /// Posts data to server using POST method
  42. /// </summary>
  43. /// <returns></returns>
  44. public string Post()
  45. {
  46. string postData = String.Empty;
  47.  
  48. int keyscount = Inputs.Keys.Count;
  49.  
  50. for (int i = 0; i < keyscount; i++)
  51. postData += "&" + Inputs.Keys[i] + "=" + HttpUtility.UrlEncode(Inputs[Inputs.Keys[i]]);
  52.  
  53. if (postData.Length > 0)
  54. postData = postData.Substring(1);
  55.  
  56. HttpWebRequest wr = WebRequest.Create(Url) as HttpWebRequest;
  57. wr.Method = "POST";
  58. wr.ContentType = "application/x-www-form-urlencoded";
  59.  
  60. using (StreamWriter requestWriter = new StreamWriter(wr.GetRequestStream()))
  61. {
  62. requestWriter.Write(postData);
  63. }
  64.  
  65. return GetResponse(wr);
  66. }
  67.  
  68. /// <summary>
  69. /// Posts data to server using GET method
  70. /// </summary>
  71. /// <returns></returns>
  72. public string Get()
  73. {
  74. string postData = String.Empty;
  75.  
  76. int keyscount = Inputs.Keys.Count;
  77.  
  78. for (int i = 0; i < keyscount; i++)
  79. postData += "&" + Inputs.Keys[i] + "=" + HttpUtility.UrlEncode(Inputs[Inputs.Keys[i]]);
  80.  
  81. if (postData.Length > 0)
  82. postData = postData.Substring(1);
  83.  
  84. string postUrl = Url.Contains("?") ? (Url + "&" + postData) : (Url + "?" + postData);
  85.  
  86. HttpWebRequest wr = WebRequest.Create(postUrl) as HttpWebRequest;
  87. wr.Method = "GET";
  88.  
  89. return GetResponse(wr);
  90. }
  91.  
  92. private string GetResponse(HttpWebRequest wr)
  93. {
  94. wr.Timeout = Timeout * 1000; //millisecond timeout
  95.  
  96. HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
  97.  
  98. if (response.StatusCode != HttpStatusCode.OK)
  99. return String.Empty;
  100.  
  101. using (Stream responseStream = response.GetResponseStream())
  102. {
  103. using (StreamReader readerStream = new StreamReader(responseStream, System.Text.Encoding.UTF8))
  104. {
  105. return readerStream.ReadToEnd();
  106. }
  107. }
  108. }
  109. }

Report this snippet  

You need to login to post a comment.