Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. public enum httpVerb
  2.     {
  3.         GET, POST, PUT, DELETE
  4.     }
  5.  
  6.     class RestClient
  7.     {
  8.         public string endPoint { get; set; }
  9.         public httpVerb httpMethod { get; set; }
  10.  
  11.         public RestClient()
  12.         {
  13.             endPoint = string.Empty;
  14.             httpMethod = httpVerb.GET;
  15.         }
  16.  
  17.         public string MakeRequest()
  18.         {
  19.             string strResponseValue = string.Empty;
  20.  
  21.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
  22.  
  23.             request.Method = httpMethod.ToString();
  24.  
  25.             using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  26.             {
  27.                 if (response.StatusCode != HttpStatusCode.OK)
  28.                 {
  29.                     throw new ApplicationException("Error:" + response.StatusCode);
  30.                 }
  31.            
  32.  
  33.                 using (Stream responseStream = response.GetResponseStream())
  34.                 {
  35.                     if (responseStream != null)
  36.                     {
  37.                         using (StreamReader reader = new StreamReader(responseStream))
  38.                         {
  39.                             strResponseValue = reader.ReadToEnd();
  40.                         }//end of stream
  41.                     }
  42.                 }
  43.  
  44.             }
  45.  
  46.             return strResponseValue;
  47.         }
  48.  
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement