using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Runtime.Serialization.Json; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Platform.Facilitys { public static class HttpHelper { static readonly HttpClient _client; static HttpHelper() { _client = new HttpClient(); } public static TOut PostSync<TIn, TOut>(ApiHost host, TIn postData) where TIn : class where TOut : class { var uri = new Uri(host.Domain); var url = new Uri(uri, host.ApiName); var response = _client.PostAsync(url, new StringContent(JsonConvert.SerializeObject(postData),Encoding.UTF8,host.DataMimeType)).Result; response.EnsureSuccessStatusCode(); return response.Content.DesObj<TOut>(); } public static Task<TOut> PostAsync<TIn, TOut>(ApiHost host, TIn postData) where TIn : class where TOut : class { var uri = new Uri(host.Domain); var url = new Uri(uri, host.ApiName); return _client.PostAsync(url, new StringContent(JsonConvert.SerializeObject(postData), Encoding.UTF8, host.DataMimeType)).GetResult<TOut>(); } public static Task<TOut> GetAsync<TOut>(ApiHost host) where TOut : class { var uri = new Uri(host.Domain); var url = new Uri(uri, host.ApiName); return _client.GetAsync(url).GetResult<TOut>(); } static Task<TOut> GetResult<TOut>(this Task<HttpResponseMessage> tsk) where TOut : class { return tsk.ContinueWith( res => { return res .Result .EnsureSuccessStatusCode() .Content .DesObj<TOut>(); }, TaskContinuationOptions.OnlyOnRanToCompletion); } static TOut DesObj<TOut>(this HttpContent content) where TOut : class { var strJson = content.ReadAsStringAsync().Result; if (!string.IsNullOrWhiteSpace(strJson)) { if (strJson.IndexOf("k__BackingField") > 0) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(TOut)); using (var mStream = new MemoryStream(Encoding.Default.GetBytes(strJson))) { return ser.ReadObject(mStream) as TOut; } } else { return JsonConvert.DeserializeObject<TOut>(strJson); } } return default(TOut); } } /// <summary> /// WebApi配置 /// </summary> public class ApiHost { #region public ApiHost() { DataMimeType = "application/json"; } public ApiHost(string domain, string apiName, string inputQueryString, string dataMimeType) { Domain = domain; ApiName = apiName; InputQueryString = inputQueryString; DataMimeType = dataMimeType; } #endregion #region /// <summary> /// 主域名(比如http://api.rongzi.com or http://127.0.0.1) /// </summary> public string Domain { get; set; } /// <summary> /// Api名稱(api/v1/studentinfotest) /// </summary> public string ApiName { get; set; } /// <summary> /// 傳入參數名稱 /// </summary> public string InputQueryString { get; set; } /// <summary> /// 返回數據類型 /// </summary> public string DataMimeType { get; set; } /// <summary> /// 重寫ToString() /// </summary> /// <returns></returns> public override string ToString() { return string.Format("{0}|{1}|{2}|{3}", this.ApiName, this.Domain, this.DataMimeType, this.InputQueryString); } #endregion } }
6)關於線程存儲 我們在開發的過程中,有時候在一個web生命周期內會需要從前到後傳遞一些對象之類的東西,這個時候可能會用到一個手段CallContext.SetData來設置數據,這個方法是將內容存放到線程中,所以只要在同一個線程中都可以取到這個內容,但是如果這個Action是異步的,async的,就會導致會有多個線程依次處理這個請求,這種情況往往是取不到之前設置的值的,這時候就可以使用CallContext.LogicSetData來設置值,這種方式設置的內容會被拷貝到新線程中