Example #1
0
        public static string Post_HttpClientHandler <Parameter>(ActionEnum RequesrAction, Parameter RequestParameter)
        {
            string            reResponse = string.Empty;
            string            para       = RequestParameter.ToSerialize();
            HttpClientHandler handler    = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            };

            using (var http = new HttpClient(handler))
            {
                HttpRequestHeaders header    = http.DefaultRequestHeaders;
                string             timeStamp = GetTimeStamp();
                string             nonce     = GetRandom();
                //加入头信息
                header.Add("staffid", staffId.ToString());                              //当前请求用户StaffId
                header.Add("timestamp", timeStamp);                                     //发起请求时的时间戳(单位:毫秒)
                header.Add("nonce", nonce);                                             //发起请求时的时间戳(单位:毫秒)
                header.Add("signature", GetSignature(timeStamp, nonce, staffId, null)); //当前请求内容的数字签名

                var parameter = RequestParameter.GetObjectPropertyValue();
                var content   = new FormUrlEncodedContent(parameter);
                var response  = http.PostAsync(RequesrAction.GetRequestApiUrlPath(), content).Result;
                if (response.IsSuccessStatusCode)
                {
                    reResponse = response.Content.ReadAsStringAsync().Result;
                }
            }
            return(reResponse);
        }
Example #2
0
        public static string Post <Parameter>(ActionEnum RequesrAction, Parameter RequestParameter)
        {
            string para      = RequestParameter.ToSerialize();
            string strResult = string.Empty;

            #region HttpWebRequest
            try
            {
                byte[]         bytes     = Encoding.UTF8.GetBytes(para);
                HttpWebRequest request   = (HttpWebRequest)WebRequest.Create(RequesrAction.GetRequestApiUrlPath());
                string         timeStamp = GetTimeStamp();
                string         nonce     = GetRandom();
                //加入头信息
                request.Headers.Add("staffid", staffId.ToString());                              //当前请求用户StaffId
                request.Headers.Add("timestamp", timeStamp);                                     //发起请求时的时间戳(单位:毫秒)
                request.Headers.Add("nonce", nonce);                                             //发起请求时的时间戳(单位:毫秒)
                request.Headers.Add("signature", GetSignature(timeStamp, nonce, staffId, para)); //当前请求内容的数字签名

                //写数据
                request.Method        = "POST";
                request.ContentLength = bytes.Length;
                request.ContentType   = ContentType;
                Stream reqstream = request.GetRequestStream();
                reqstream.Write(bytes, 0, bytes.Length);

                //读数据
                request.Timeout = 300000;
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response      = (HttpWebResponse)request.GetResponse();
                Stream          streamReceive = response.GetResponseStream();
                StreamReader    streamReader  = new StreamReader(streamReceive, Encoding.UTF8);
                strResult = streamReader.ReadToEnd();

                //关闭流
                reqstream.Close();
                streamReader.Close();
                streamReceive.Close();
                request.Abort();
                response.Close();
            }
            catch (Exception ex)
            {
                strResult = Post_HttpClientHandler(RequesrAction, RequestParameter);
            }
            #endregion
            return(strResult);
        }