Advertisement
Guest User

Untitled

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