コード例 #1
0
        private ApiResponseObject ExecuteAction(MembershipApiMethodEnum method, NameValueCollection parameters, bool exceptionOnErrorCode = true)
        {
            parameters.Add("key", _apiPrivateKey);
            var queryString = ToQueryString(parameters);
            var uri         = BuildPathToTheAction(method);

            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] bytes = Encoding.ASCII.GetBytes(queryString);
            request.ContentLength = bytes.Length;
            using (var stream = request.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);
            }

            string responseText;

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    using (var answerReader = new StreamReader(response.GetResponseStream()))
                    {
                        responseText = answerReader.ReadToEnd();
                    }
                }
            }
            catch (Exception e)
            {
                throw new MembershipApiException(String.Format("Membership api: Unable to receive response from the following server: {0} | Server log: {1}", _apiBaseUrl, e.ToString()), MembershipApiErrorCodesEnum.ApiCallServerError, e.ToString());
            }

            var item = JsonConvert.DeserializeObject <ApiResponseObject>(responseText);

            if (item.error_code == MembershipApiErrorCodesEnum.InvalidApiKey)
            {
                throw new MembershipApiException(String.Format("Membership api: Invalid api key provided."), item.error_code, String.Empty);
            }
            else if (item.status == ApiResponseStatusEnum.Error &&
                     item.error_code != MembershipApiErrorCodesEnum.ModelStateErrors &&
                     exceptionOnErrorCode)
            {
                throw new MembershipApiException(String.Format("Membership api: Remote server returned following error code: {0}, server log:  {1}",
                                                               item.error_code,
                                                               item.log
                                                               ), item.error_code, item.log);
            }

            return(item);
        }
コード例 #2
0
 private Uri BuildPathToTheAction(MembershipApiMethodEnum method)
 {
     return(new Uri(String.Format("{0}/{1}", _apiBaseUrl, method)));
 }