c# http請求封裝
阿新 • • 發佈:2019-01-06
看到別人寫的程式碼copy一下,作為備份。
/// <summary> /// Restful客戶端 /// </summary> public class RESTClient { // 常量 private const string HEAD_KEY = "Authorization"; private const string TOKEN_KEY_PREFIX = "SmartTracking"; // Restful訪問基地址 private string baseUrl = string.Empty; /// <summary> /// Restful訪問基地址 /// </summary> public string BaseUrl { get { return baseUrl; } set { baseUrl = value; } } // 令牌鑰匙 private string tokenKey = string.Empty; /// <summary> /// 令牌鑰匙 /// </summary> public string TokenKey { set { tokenKey = value; } } // 訪問超時時間 private long timeout; /// <summary> /// 訪問超時時間 /// </summary> public long Timeout { get { return timeout; } set { timeout = value; } } /// <summary> /// 建構函式 /// </summary> public RESTClient() { } /// <summary> /// 請求GET方法 /// </summary> /// <param name="url"></param> /// <returns></returns> public RESTResponseEntity RequestGet(string url) { RESTResponseEntity entity = new RESTResponseEntity(); try { HttpClient httpClient = new HttpClient(); if(timeout > 0){ httpClient.Timeout = TimeSpan.FromMilliseconds(timeout); } httpClient.DefaultRequestHeaders.Add(HEAD_KEY, TOKEN_KEY_PREFIX + " " + tokenKey); string strUrl = baseUrl + url; HttpResponseMessage hrm = httpClient.GetAsync(strUrl).Result; entity.StatusCode = Convert.ToInt32(hrm.StatusCode); entity.Buffer = hrm.Content.ReadAsByteArrayAsync().Result; } catch (Exception ex) { Log.WriteLogFile(ex, "RESTClient RequestGet() Error"); } return entity; } /// <summary> /// 請求POST方法 /// </summary> /// <param name="url"></param> /// <returns></returns> public RESTResponseEntity RequestPOST(string url,List<KeyValuePair<string, string>> paras) { RESTResponseEntity entity = new RESTResponseEntity(); HttpClient httpClient = null; FormUrlEncodedContent content = null; HttpResponseMessage hrm = null; try { httpClient = new HttpClient(); if (timeout > 0) { httpClient.Timeout = TimeSpan.FromMilliseconds(timeout); } httpClient.DefaultRequestHeaders.Add(HEAD_KEY, TOKEN_KEY_PREFIX + " "+ tokenKey); content = new FormUrlEncodedContent(paras); string strUrl = baseUrl + url; hrm = httpClient.PostAsync(strUrl, content).Result; entity.StatusCode = Convert.ToInt32(hrm.StatusCode); entity.Buffer = hrm.Content.ReadAsByteArrayAsync().Result; httpClient.Dispose(); } catch (Exception ex) { Log.WriteLogFile(ex, "RESTClient RequestPOST() Error"); //Console.WriteLine("RESTClient RequestPOST():" + ex.Message); } if (httpClient != null) { httpClient.Dispose(); httpClient = null; } if (content != null) { content.Dispose(); content = null; } if (hrm != null) { hrm.Dispose(); hrm = null; } Console.WriteLine(Thread.CurrentThread.ManagedThreadId); return entity; } /// <summary> /// 請求POST方法 /// </summary> /// <param name="url"></param> /// <returns></returns> public RESTResponseEntity RequestMulPOST(string url, List<KeyValuePair<string, string>> paras) { RESTResponseEntity entity = new RESTResponseEntity(); HttpClient httpClient = null; FormUrlEncodedContent content = null; MultipartFormDataContent mulcontnt = null; HttpResponseMessage hrm = null; try { httpClient = new HttpClient(); if (timeout > 0) { httpClient.Timeout = TimeSpan.FromMilliseconds(timeout); } httpClient.DefaultRequestHeaders.Add(HEAD_KEY, TOKEN_KEY_PREFIX + " " + tokenKey); content = new FormUrlEncodedContent(paras); mulcontnt = new MultipartFormDataContent(); string strUrl = baseUrl + url; hrm = httpClient.PostAsync(strUrl, content).Result; entity.StatusCode = Convert.ToInt32(hrm.StatusCode); entity.Buffer = hrm.Content.ReadAsByteArrayAsync().Result; httpClient.Dispose(); } catch (Exception ex) { Log.WriteLogFile(ex, "RESTClient RequestPOST() Error"); //Console.WriteLine("RESTClient RequestPOST():" + ex.Message); } if (httpClient != null) { httpClient.Dispose(); httpClient = null; } if (content != null) { content.Dispose(); content = null; } if (hrm != null) { hrm.Dispose(); hrm = null; } Console.WriteLine(Thread.CurrentThread.ManagedThreadId); return entity; } /// <summary> /// 請求PUT方法 /// </summary> /// <param name="url"></param> /// <returns></returns> public RESTResponseEntity RequestPUT(string url, List<KeyValuePair<string, string>> paras) { RESTResponseEntity entity = new RESTResponseEntity(); try { HttpClient httpClient = new HttpClient(); if (timeout > 0) { httpClient.Timeout = TimeSpan.FromMilliseconds(timeout); } httpClient.DefaultRequestHeaders.Add(HEAD_KEY, TOKEN_KEY_PREFIX + " " + tokenKey); FormUrlEncodedContent content = new FormUrlEncodedContent(paras); string strUrl = baseUrl + url; HttpResponseMessage hrm = httpClient.PutAsync(strUrl, content).Result; entity.StatusCode = Convert.ToInt32(hrm.StatusCode); entity.Buffer = hrm.Content.ReadAsByteArrayAsync().Result; } catch (Exception ex) { Log.WriteLogFile(ex, "RESTClient RequestPUT() Error"); } return entity; } /// <summary> /// 請求DELETE方法 /// </summary> /// <param name="url"></param> /// <returns></returns> public RESTResponseEntity RequestDELETE(string url) { RESTResponseEntity entity = new RESTResponseEntity(); try { HttpClient httpClient = new HttpClient(); if (timeout > 0) { httpClient.Timeout = TimeSpan.FromMilliseconds(timeout); } httpClient.DefaultRequestHeaders.Add(HEAD_KEY, TOKEN_KEY_PREFIX + " " + tokenKey); string strUrl = baseUrl + url; HttpResponseMessage hrm = httpClient.DeleteAsync(strUrl).Result; entity.StatusCode = Convert.ToInt32(hrm.StatusCode); entity.Buffer = hrm.Content.ReadAsByteArrayAsync().Result; } catch (Exception ex) { Log.WriteLogFile(ex, "RESTClient RequestDELETE() Error"); } return entity; } }