Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RestHelper
- {
- protected Dictionary<string, string> dicRestHeaders;
- public RestHelper()
- {
- this.dicRestHeaders = new Dictionary<string, string>();
- }
- public Dictionary<string, string> RestHeaders
- {
- get { return this.dicRestHeaders; }
- set { this.dicRestHeaders = value; }
- }
- public string CallGetRestMethod(string url)
- {
- Dictionary<string, string> dicRespondeHeaders = null;
- return this.CallGetRestMethod(url, out dicRespondeHeaders);
- }
- public string CallGetRestMethod(string url, out Dictionary<string,string> dicRespondeHeaders)
- {
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
- webRequest.Method = "GET";
- webRequest.ContentType = "application/json";
- foreach (string key in this.dicRestHeaders.Keys)
- {
- webRequest.Headers.Add(key, this.dicRestHeaders[key]);
- }
- webRequest.Timeout = 2 * 60 * 1000;
- HttpWebResponse webresponse = (HttpWebResponse)webRequest.GetResponse();
- dicRespondeHeaders = new Dictionary<string, string>();
- foreach (string key in webresponse.Headers.Keys)
- {
- dicRespondeHeaders.Add(key, webresponse.Headers[key]);
- }
- Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
- StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(), enc);
- string result = string.Empty;
- result = responseStream.ReadToEnd();
- webresponse.Close();
- return result;
- }
- public T CallGetRestMethod<T>(string url)
- where T : class
- {
- string json = this.CallGetRestMethod(url);
- T jsonConverted = JsonConvert.DeserializeObject<T>(json);
- return jsonConverted;
- }
- public T CallGetRestMethod<T>(string url, out Dictionary<string, string> dicRespondeHeaders)
- where T : class
- {
- string json = this.CallGetRestMethod(url, out dicRespondeHeaders);
- T jsonConverted = JsonConvert.DeserializeObject<T>(json);
- return jsonConverted;
- }
- public string CallPostRestMethod(string url, string jsonContent)
- {
- MyWebClient webClient = new MyWebClient();
- webClient.Headers.Add("Content-Type", "application/json");
- foreach (string key in this.dicRestHeaders.Keys)
- {
- webClient.Headers.Add(key, this.dicRestHeaders[key]);
- }
- string response = webClient.UploadString(url, jsonContent);
- return response;
- }
- public byte[] CallUploadRestMethod(string url, string fileName)
- {
- MyWebClient webClient = new MyWebClient();
- foreach (string key in this.dicRestHeaders.Keys)
- {
- webClient.Headers.Add(key, this.dicRestHeaders[key]);
- }
- if (this.UploadProgressChanged != null)
- {
- webClient.UploadProgressChanged += (sender, e) =>
- {
- this.UploadProgressChanged(sender, e);
- };
- }
- if (this.UploadFileCompleted != null)
- {
- webClient.UploadFileCompleted += (sender, e) =>
- {
- this.UploadFileCompleted(sender, e);
- };
- }
- byte[] response = webClient.UploadFile(url, "PUT", fileName);
- return response;
- }
- public event UploadProgressChangedEventHandler UploadProgressChanged;
- public event UploadFileCompletedEventHandler UploadFileCompleted;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement