Пример #1
0
        /// <summary>
        /// 请求协议
        /// </summary>
        /// <param name="api">API方法名</param>
        /// <typeparam name="T"></typeparam>
        /// <returns>返回的包结构</returns>
        public TAPDResponse <T> Request <T>(string api = "", TAPDRequest request = null, JsonConverter converter = null)
        {
            string path = "";

            string data = "";

            TAPDHttpMethod method = TAPDHttpMethod.Get;

            //拼接参数
            data = TAPDHttp.JoinHttpParameters <TAPDRequest>(m_WorkspaceID, request);

            if (string.IsNullOrEmpty(api))
            {
                path = TAPDHttpAPI.BASE_URL;
            }
            else
            {
                path = string.Format("{0}{1}", TAPDHttpAPI.BASE_URL, api);
            }

            if (request != null)
            {
                //获取请求的http参数
                TAPDHttpAttribute httpAttribute = ReflectionUtil.GetCustomAttributes <TAPDHttpAttribute>(request.GetType());

                if (httpAttribute != null)
                {
                    method = httpAttribute.method;
                }
            }

            TAPDResponse <T> response;

            if (converter != null)
            {
                response = TAPDHttp.Request <T>(path, converter, authorization, data, method);
            }
            else
            {
                response = TAPDHttp.Request <T>(path, authorization, data, method);
            }

            return(response);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="method"></param>
        /// <param name="contentType"></param>
        public static string Request(string url, string authorization = "", string data = "", TAPDHttpMethod method = TAPDHttpMethod.Get, string contentType = "")
        {
            string content = "";

            HttpWebRequest webRequest = null;

            try
            {
                switch (method)
                {
                case TAPDHttpMethod.Get:
                    webRequest = CreateGetRequest(url, authorization, data);
                    break;

                case TAPDHttpMethod.Post:
                    webRequest = CreatePostRequest(url, authorization, data);
                    break;

                default:
                    return("");
                }

                //webRequest.Method = Enum.GetName(typeof(TAPDHttpMethod), method);
                //webRequest.Headers.Add("Authorization", authorization);
                webRequest.ContentType = contentType;

                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;

                content = ReadWebResponse(webResponse);
            }
            catch (WebException webException)
            {
                HttpWebResponse webResponse = webException.Response as HttpWebResponse;

                content = ReadWebResponse(webResponse);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (webRequest != null)
                {
                    webRequest.Abort();
                }
            }

            return(content);
        }
Пример #3
0
 public TAPDHttpAttribute(TAPDHttpMethod method)
 {
     this.method = method;
 }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="method"></param>
        /// <param name="contentType"></param>
        public static TAPDResponse <T> Request <T>(string url, string authorization = "", string data = "", TAPDHttpMethod method = TAPDHttpMethod.Get, string contentType = "")
        {
            string content = Request(url, authorization, data, method, contentType);

            TAPDResponse <T> result = JsonConvert.DeserializeObject <TAPDResponse <T> >(content);

            return(result);
        }