예제 #1
0
        /// <summary>
        /// send http request with POST method
        /// </summary>
        /// <param name="methodName">请求的接口名称</param>
        /// <param name="postData">请求数据</param>
        /// <param name="authorization">签名</param>
        /// <returns></returns>
        public static string HttpPost(string methodName, string postData, string authorization)
        {
            string ret = string.Empty;

            try
            {
                byte[]         byteArray = Encoding.UTF8.GetBytes(postData); //转化为UTF8
                HttpWebRequest webReq    = null;

                if (Conf.Instance().END_POINT.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    webReq = WebRequest.Create((Conf.Instance().END_POINT + methodName)) as HttpWebRequest;
                    webReq.ProtocolVersion = HttpVersion.Version10;
                }
                else
                {
                    webReq = (HttpWebRequest)WebRequest.Create(new Uri(Conf.Instance().END_POINT + methodName));
                }


                webReq.Method      = "POST";
                webReq.ContentType = "text/json";
                webReq.Headers.Add(HttpRequestHeader.Authorization, authorization);
                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader    sr       = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    var response = ex.Response as HttpWebResponse;
                    if (response != null)
                    {
                        int errorcode = (int)response.StatusCode;
                        ret = errorcode.ToString();// Youtu.statusText(errorcode);
                    }
                    else
                    {
                        // no http status code available
                    }
                }
                else
                {
                    // no http status code available
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(ret);
        }
예제 #2
0
        /// <summary>
        /// 签名
        /// </summary>
        /// <param name="expired">过期时间</param>
        /// <param name="userid">暂时不用</param>
        /// <returns>签名</returns>
        public static string appSign(string expired, string userid)
        {
            if (string.IsNullOrEmpty(Conf.Instance().SECRET_ID) || string.IsNullOrEmpty(Conf.Instance().SECRET_KEY))
            {
                return(AUTH_SECRET_ID_KEY_ERROR);
            }

            string time = Utility.UnixTime();

            string plainText = SetOrignal(Conf.Instance().USER_ID, Conf.Instance().APPID, Conf.Instance().SECRET_ID, time, expired);

            byte[] signByteArrary = Utility.JoinByteArr(HmacSha1Sign(plainText, Conf.Instance().SECRET_KEY), Utility.StrToByteArr(plainText));

            return(Convert.ToBase64String(signByteArrary));
        }