コード例 #1
0
        public ApiResult UserQuery(string path, HttpMethod httpMethod, Dictionary <string, string> headers = null, string content = "", bool jsonContent = false)
        {
            xNet.HttpResponse response    = null;
            string            contentType = null;

            path = BaseUri + path;

            if (!(httpMethod == HttpMethod.Get || httpMethod == HttpMethod.Post ||
                  httpMethod == HttpMethod.Delete || httpMethod == HttpMethod.Put))
            {
                throw new ArgumentException("Unsupported http method " + httpMethod);
            }
            if (httpMethod != HttpMethod.Get && content.Length == 0)
            {
                throw new ArgumentException("Empty content.");
            }

            if (headers != null && headers.Count == 0)
            {
                throw new ArgumentException("Empty headers.");
            }

            if (headers != null)
            {
                foreach (var h in headers)
                {
                    _request.AddHeader(h.Key, h.Value);
                }
            }
            if (httpMethod != HttpMethod.Get)
            {
                contentType = jsonContent ? "application/json" : "application/x-www-form-urlencoded";
            }
            lock (_obj)
            {
                try
                {
                    if (httpMethod == HttpMethod.Get)
                    {
                        response = _request.Get(path);
                    }

                    else if (httpMethod == HttpMethod.Post)
                    {
                        response = _request.Post(path, content, contentType);
                    }

                    else if (httpMethod == HttpMethod.Delete)
                    {
                        response = _request.Delete(path, content, contentType);
                    }

                    else if (httpMethod == HttpMethod.Put)
                    {
                        response = _request.Put(path, content, contentType);
                    }
                }
                catch (xNet.HttpException ex)
                {
                    return(new ApiResult((int)ex.HttpStatusCode, ex.InnerMessage));
                }
            }
            content = response.ToString();
            ParseResponseHeaders(response);

            return(new ApiResult((int)response.StatusCode, content));
        }