C# HttpHelper

2021-07-01 10:06

阅读:1310

标签:summary   .post   ESS   throw   content   out   accept   write   failed   

    public enum HttpVerb
    {
        Get,
        Post
    }
    
    public class HttpHelper
    {
        private string _contentType = "application/x-www-form-urlencoded";
        //private string _contentType = "application/json";
        //private string _contentType = "text/xml";
        private int _timeout = 15000;
        
        public string EndPoint { get; set; }
        public HttpVerb Method { get; set; }
        public string ContentType { get { return _contentType; } set { _contentType = value; } }
        public string PostData { get; set; }

        /// 
        /// 默认15000毫秒
        /// 
        public int Timeout { get { return _timeout; } set { _timeout = value; } }
        
        public HttpHelper(string endpoint)
        {
            EndPoint = endpoint;
            Method = HttpVerb.Get;
            ContentType = _contentType;
            PostData = "{}";
        }
        
        public string Post(string data)
        {
            var service = new Uri(EndPoint);
            var request = WebRequest.CreateHttp(service);
            request.Method = "POST";
            request.Timeout = Timeout;
            request.KeepAlive = true;
            request.ContentLength = 0;
            request.ContentType = ContentType;
            request.Accept = "application/json";

            var bytes = Encoding.UTF8.GetBytes(data);
            request.ContentLength = bytes.Length;
            using (var writeStream = request.GetRequestStream())
            {
                writeStream.Write(bytes, 0, bytes.Length);
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = string.Empty;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
                    throw new WebException(message);
                }
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                }
                return responseValue;
            }
        }

        /// 
        /// Request server
        /// 
        /// eg. "?q=1234"
        /// 
        public string Get(string parameters)
        {
            var service = new Uri(EndPoint + parameters);
            var request = WebRequest.CreateHttp(service);
            request.Method = "GET";
            request.Timeout = Timeout;
            request.KeepAlive = true;
            request.ContentLength = 0;
            request.ContentType = ContentType;

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = string.Empty;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
                    throw new WebException(message);
                }
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                }
                return responseValue;
            }
        }

        /// 
        /// 下载更新二进制文件
        /// 
        /// eg. "?q=1234"
        /// 
        public byte[] DownLoad(string parameters)
        {
            var service = new Uri(EndPoint + parameters);
            var request = WebRequest.CreateHttp(service);
            request.Timeout = Timeout;
            request.KeepAlive = true;
            request.Method = Method.ToString().ToUpper(CultureInfo.CurrentCulture);
            request.ContentLength = 0;
            request.ContentType = ContentType;

            if (Method == HttpVerb.Post)
            {
                var bytes = Encoding.UTF8.GetBytes(PostData);
                request.ContentLength = bytes.Length;

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
                    throw new WebException(message);
                }
                using (var result = new MemoryStream())
                {
                    const int bufferLen = 2048;
                    var buffer = new byte[bufferLen];
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                        {
                            var len = responseStream.Read(buffer, 0, bufferLen);
                            while (len > 0)
                            {
                                result.Write(buffer, 0, len);
                                len = responseStream.Read(buffer, 0, bufferLen);
                            }
                        }
                    }
                    return result.ToArray();
                }
            }
        }

    }

 

C# HttpHelper

标签:summary   .post   ESS   throw   content   out   accept   write   failed   

原文地址:https://www.cnblogs.com/jonney-wang/p/9953008.html


评论


亲,登录后才可以留言!