Beispiel #1
0
        /// <summary>
        /// 创建并开始异步请求数据。
        /// </summary>
        /// <param name="contentType">设置 Content-Type Http 标头的值。</param>
        /// <param name="method">设置请求的方法。</param>
        /// <param name="url">标识 Internet 资源的 URI。</param>
        /// <param name="body">请求的内容体。</param>
        /// <param name="action">调用成功的回调函数。</param>
        /// <exception cref="System.ArgumentNullException">[HttpClient].[Request].contentType or
        /// [HttpClient].[Request].url or [HttpClient].[Request].method</exception>
        public void Request(string contentType, string method, string url, byte[] body, Action <ResponseInfo> action)
        {
            if (body == null)
            {
                body = new byte[0];
            }

            if (contentType.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException("[HttpClient].[Request].contentType");
            }

            if (url.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException("[HttpClient].[Request].url");
            }
            else if (!BaseDirectory.IsNullOrWhiteSpace())
            {
                var hasAppendBase = BaseDirectory.EndsWith("//");
                var hasInsertUrl  = url.StartsWith("//");
                if (!(hasAppendBase || hasInsertUrl))
                {
                    url = string.Concat(BaseDirectory, "//", url);
                }
                else
                {
                    url = string.Concat(BaseDirectory, url);
                }
            }

            if (method.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException("[HttpClient].[Request].method");
            }
            else
            {
                method = method.ToUpper();
            }

            if (method != "GET" && method != "POST" && method != "DELETE" && method != "PUT")
            {
                throw new ArgumentException("[HttpClient].[Request].method is invalid method.");
            }

            var client = (HttpWebRequest)WebRequest.Create(new Uri(url));

            client.CookieContainer = Cookies;
            client.Method          = method;
            client.Accept          = Accept;
            client.ContentType     = contentType;
            client.UserAgent       = UserAgent;
            client.Proxy           = Proxy;

            if (Setting != null)
            {
                client.AutomaticDecompression = Setting.DecompressionMethod;
                client.Credentials            = Setting.Credentials;
            }

            foreach (var item in Headers)
            {
                client.Headers.Add(item.Key, item.Value);
            }

            if (method == "POST" || method == "PUT" || method == "DELETE")
            {
                using (var stream = client.GetRequestStream())
                {
                    stream.Write(body, 0, body.Length);
                    stream.Flush();
                }
            }

            using (var response = GetResponseObject(client.GetResponse() as HttpWebResponse))
            {
                action(response);
            }
        }