Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. public class RestHelper
  2.     {
  3.         protected Dictionary<string, string> dicRestHeaders;
  4.  
  5.         public RestHelper()
  6.         {
  7.             this.dicRestHeaders = new Dictionary<string, string>();
  8.         }
  9.  
  10.         public Dictionary<string, string> RestHeaders
  11.         {
  12.             get { return this.dicRestHeaders; }
  13.             set { this.dicRestHeaders = value; }
  14.         }
  15.  
  16.         public string CallGetRestMethod(string url)
  17.         {
  18.             Dictionary<string, string> dicRespondeHeaders = null;
  19.             return this.CallGetRestMethod(url, out dicRespondeHeaders);
  20.         }
  21.  
  22.         public string CallGetRestMethod(string url, out Dictionary<string,string> dicRespondeHeaders)
  23.         {
  24.             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
  25.             webRequest.Method = "GET";
  26.             webRequest.ContentType = "application/json";
  27.  
  28.             foreach (string key in this.dicRestHeaders.Keys)
  29.             {
  30.                 webRequest.Headers.Add(key, this.dicRestHeaders[key]);
  31.             }
  32.  
  33.             webRequest.Timeout = 2 * 60 * 1000;
  34.             HttpWebResponse webresponse = (HttpWebResponse)webRequest.GetResponse();
  35.  
  36.             dicRespondeHeaders = new Dictionary<string, string>();
  37.             foreach (string key in webresponse.Headers.Keys)
  38.             {
  39.                 dicRespondeHeaders.Add(key, webresponse.Headers[key]);
  40.             }
  41.  
  42.             Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
  43.             StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(), enc);
  44.             string result = string.Empty;
  45.             result = responseStream.ReadToEnd();
  46.             webresponse.Close();
  47.             return result;
  48.         }
  49.  
  50.         public T CallGetRestMethod<T>(string url)
  51.             where T : class
  52.         {
  53.             string json = this.CallGetRestMethod(url);
  54.             T jsonConverted = JsonConvert.DeserializeObject<T>(json);
  55.             return jsonConverted;
  56.         }
  57.         public T CallGetRestMethod<T>(string url, out Dictionary<string, string> dicRespondeHeaders)
  58.             where T : class
  59.         {
  60.             string json = this.CallGetRestMethod(url, out dicRespondeHeaders);
  61.             T jsonConverted = JsonConvert.DeserializeObject<T>(json);
  62.             return jsonConverted;
  63.         }
  64.  
  65.         public string CallPostRestMethod(string url, string jsonContent)
  66.         {
  67.             MyWebClient webClient = new MyWebClient();
  68.             webClient.Headers.Add("Content-Type", "application/json");
  69.  
  70.             foreach (string key in this.dicRestHeaders.Keys)
  71.             {
  72.                 webClient.Headers.Add(key, this.dicRestHeaders[key]);
  73.             }
  74.  
  75.             string response = webClient.UploadString(url, jsonContent);
  76.  
  77.             return response;
  78.         }
  79.  
  80.         public byte[] CallUploadRestMethod(string url, string fileName)
  81.         {
  82.             MyWebClient webClient = new MyWebClient();
  83.  
  84.             foreach (string key in this.dicRestHeaders.Keys)
  85.             {
  86.                 webClient.Headers.Add(key, this.dicRestHeaders[key]);
  87.             }
  88.  
  89.             if (this.UploadProgressChanged != null)
  90.             {
  91.                 webClient.UploadProgressChanged += (sender, e) =>
  92.                 {
  93.                     this.UploadProgressChanged(sender, e);
  94.                 };
  95.             }
  96.             if (this.UploadFileCompleted != null)
  97.             {
  98.                 webClient.UploadFileCompleted += (sender, e) =>
  99.                 {
  100.                     this.UploadFileCompleted(sender, e);
  101.                 };
  102.             }
  103.  
  104.             byte[] response = webClient.UploadFile(url, "PUT", fileName);
  105.  
  106.             return response;
  107.         }
  108.  
  109.         public event UploadProgressChangedEventHandler UploadProgressChanged;
  110.         public event UploadFileCompletedEventHandler UploadFileCompleted;
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement