public static IPostContentModel GetInitializedFactory <T>(PostContentTypeEnum contentType, int order, params T[] content) { IPostContentModel resultFactory = null; switch (contentType) { case PostContentTypeEnum.Audio: resultFactory = new AudioPostContentModel(order, content.Cast <AudioPostContentContainerModel>().ToArray()); break; case PostContentTypeEnum.Img: resultFactory = new ImgPostContentModel(order, content.Cast <string>().ToArray()); break; case PostContentTypeEnum.Text: resultFactory = new TextPostContentModel(order, content.Cast <string>().ToArray()); break; case PostContentTypeEnum.Video: resultFactory = new VideoPostContentModel(order, content.Cast <VideoPostContentContainerModel>().ToArray()); break; } return(resultFactory); }
public async Task <string> DoPostStringAsync(string url, HttpContent content, AuthenticationHeaderValue authToken = null, Dictionary <string, string> heart = null, PostContentTypeEnum contentTypeEnum = PostContentTypeEnum.URLENCODED) { var request = httpClientFactory.CreateClient(); if (authToken != null) { request.DefaultRequestHeaders.Authorization = authToken; } if (heart != null) { foreach (var item in heart) { content.Headers.Add(item.Key, item.Value); } } if (contentTypeEnum == PostContentTypeEnum.URLENCODED) { request.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); } else if (contentTypeEnum == PostContentTypeEnum.JSON) { request.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } using (var respon = await request.PostAsync(url, content)) { var res = (await respon.Content.ReadAsStringAsync());//返回结果 if (res.IsNullOrEmpty()) { logger.LogInformation($"{url},ResponMessage:{respon.ToJson()}"); } return(res); } }
/// <summary> /// post 请求 (key value 使用 FormUrlEncodedContent) json 格式使用 StringContent /// </summary> /// <param name="url">访问地址</param> /// <param name="authToken">oauth token</param> /// <param name="heart">访问头</param> /// <param name="contentTypeEnum">传输类型</param> /// <returns></returns> public async Task <Dictionary <string, object> > DoPostAsync(string url, HttpContent content, AuthenticationHeaderValue authToken = null, Dictionary <string, string> heart = null, PostContentTypeEnum contentTypeEnum = PostContentTypeEnum.URLENCODED) { var res = await DoPostStringAsync(url, content, authToken, heart, contentTypeEnum); return(res.ToDic()); }