Beispiel #1
0
        /// <summary>
        /// POST扩展请求(可Post数组)
        /// </summary>
        /// <typeparam name="T">获得响应类型</typeparam>
        /// <typeparam name="TPostModel">请求对象,WEBAPI中只有[FromBody]对象类型</typeparam>
        /// <param name="uri">请求的URL地址</param>
        /// <param name="t">请求对象,WEBAPI中只有[FromBody]对象类型</param>
        /// <param name="parms">请求的URL参数</param>
        /// <returns></returns>
        public static T PostExtendRequest
        <T, TPostModel>(string uri, TPostModel t, Dictionary <string, object> dicParms = null)
            where T : class, new()
            where TPostModel : class, new()
        {
            if (dicParms != null)
            {
                if (!uri.EndsWith("?"))
                {
                    uri = uri + "?";
                }
                foreach (KeyValuePair <string, object> k in dicParms)
                {
                    uri += string.Format("{0}={1}&", k.Key, k.Value);
                }
                uri = uri.TrimEnd('&');
            }
            //WebRequest request = WebRequest.Create(uri);
            //request.Method = "POST";
            //request.Timeout = 30000;
            //request.ContentType = "application/json";
            //request.Proxy = null;
            var _handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            };
            var _httpClient = new HttpClient(_handler);
            var content     = new StringContent(JsonConvert.SerializeObject(t));

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            content.Headers.Add("clientType", GetClientString());
            content.Headers.Add("security", ClientTokenModule.GetToken());
            var resMsg = _httpClient.PostAsync(uri, content);
            var result = resMsg.Result.Content.ReadAsStringAsync();

            resMsg.Dispose();
            var entity = new T();

            if (result != null)
            {
                entity = JsonConvert.DeserializeObject <T>(result.Result);
            }
            return(entity);
            //var stream = request.GetRequestStream();
            //string json = Newtonsoft.Json.JsonConvert.SerializeObject(t);
            //byte[] bytes = Encoding.UTF8.GetBytes(json);
            //stream.Write(bytes, 0, bytes.Length);
            //using (var response = request.GetResponse())
            //{
            //    return JsonConvert.DeserializeObject<T>(GetResponseContent(response));
            //}
        }
Beispiel #2
0
        /// <summary>
        /// POST请求(未压缩)
        /// </summary>
        /// <typeparam name="T">获得响应类型</typeparam>
        /// <typeparam name="TPostModel">请求对象,WEBAPI中只有[FromBody]对象类型</typeparam>
        /// <param name="uri">请求的URL地址</param>
        /// <param name="t">请求对象,WEBAPI中只有[FromBody]对象类型</param>
        /// <param name="parms">请求的URL参数</param>
        /// <returns></returns>
        public static T PostNoZipRequest <T, TPostModel>(string uri, TPostModel t, Dictionary <string, object> dicParms = null)
            where T : class, new()
            where TPostModel : class, new()
        {
            if (dicParms != null)
            {
                if (!uri.EndsWith("?"))
                {
                    uri = uri + "?";
                }
                foreach (KeyValuePair <string, object> k in dicParms)
                {
                    uri += string.Format("{0}={1}&", k.Key, k.Value);
                }
                uri = uri.TrimEnd('&');
            }
            WebRequest request = WebRequest.Create(uri);

            request.Method      = "POST";
            request.Timeout     = 30000;
            request.Proxy       = null;
            request.ContentType = "application/json";
            request.Headers.Add("clientType", GetClientString());
            request.Headers.Add("security", ClientTokenModule.GetToken());
            var    stream = request.GetRequestStream();
            string json   = JsonConvert.SerializeObject(t);

            byte[] bytes = Encoding.UTF8.GetBytes(json);
            stream.Write(bytes, 0, bytes.Length);
            using (var response = request.GetResponse())
            {
                var          responseStream = response.GetResponseStream();
                StreamReader reader         = new StreamReader(responseStream);
                string       responseStr    = reader.ReadToEnd();
                return(JsonConvert.DeserializeObject <T>(responseStr));
            }
        }