Advertisement
Guest User

HttpRequest

a guest
Apr 24th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. using SharpPcap;
  6. using System.Net.Http;
  7. using System.Text;
  8.  
  9. namespace BlackSniffyCore.packet
  10. {
  11. public class HttpRequest
  12. {
  13. private WebRequest request;
  14. private Stream dataStream;
  15.  
  16. private string status;
  17.  
  18. public String Status
  19. {
  20. get
  21. {
  22. return status;
  23. }
  24. set
  25. {
  26. status = value;
  27. }
  28. }
  29.  
  30.  
  31. public HttpRequest (string url)
  32. {
  33. // Create a request using a URL that can receive a post.
  34.  
  35. request = WebRequest.Create(url);
  36. }
  37.  
  38. public HttpRequest(string url, string method)
  39. : this(url)
  40. {
  41.  
  42. if (method.Equals("GET") || method.Equals("POST"))
  43. {
  44. // Set the Method property of the request to POST.
  45. request.Method = method;
  46. }
  47. else
  48. {
  49. throw new Exception("Invalid Method Type");
  50. }
  51. }
  52.  
  53. public HttpRequest(string url, string method, string data)
  54. : this(url, method)
  55. {
  56.  
  57. // Create POST data and convert it to a byte array.
  58. string postData = data;
  59. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  60.  
  61. // Set the ContentType property of the WebRequest.
  62. request.ContentType = "application/x-www-form-urlencoded";
  63.  
  64. // Set the ContentLength property of the WebRequest.
  65. request.ContentLength = byteArray.Length;
  66.  
  67. // Get the request stream.
  68. dataStream = request.GetRequestStream();
  69.  
  70. // Write the data to the request stream.
  71. dataStream.Write(byteArray, 0, byteArray.Length);
  72.  
  73. // Close the Stream object.
  74. dataStream.Close();
  75.  
  76. }
  77.  
  78. public string GetResponse()
  79. {
  80. // Get the original response.
  81. WebResponse response = request.GetResponse();
  82.  
  83. this.Status = ((HttpWebResponse)response).StatusDescription;
  84.  
  85. // Get the stream containing all content returned by the requested server.
  86. dataStream = response.GetResponseStream();
  87.  
  88. // Open the stream using a StreamReader for easy access.
  89. StreamReader reader = new StreamReader(dataStream);
  90.  
  91. // Read the content fully up to the end.
  92. string responseFromServer = reader.ReadToEnd();
  93.  
  94. // Clean up the streams.
  95. reader.Close();
  96. dataStream.Close();
  97. response.Close();
  98.  
  99. return responseFromServer;
  100. }
  101.  
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement