C# HTTP封装请求(包含Post,Get)

string url = "https://restapi.amap.com/v3/distance?origins=abc";
string res = HttpGet(url);

string url1="http://zxcvbnm/Shop/AddOrder";
string json = JsonConvert.SerializeObject(new { type = "1", order_id = "2" });
string result = HttpPost(url1, json, "application/json");


/// 
        /// Get请求
        /// 
        /// 请求地址
        /// 超时时间默认两分钟
        /// 
        public static string HttpGet(string url, int Timeout=120000)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = Timeout;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        /// 
        /// Post请求
        /// 
        /// 请求地址
        /// 请求参数
        /// 参数类型
        /// 请求头
        /// 
        public static string HttpPost(string url, string pm_str, string pm_ContentType = "appication/json", string headstr = null)
        {
            StreamReader reader = null;
            Stream requestStream = null;
            HttpWebResponse Response = null;
            try
            {
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
                ServicePointManager.DefaultConnectionLimit = int.MaxValue;

                myReq.Method = "POST";
                if (pm_ContentType != null)
                {
                    myReq.ContentType = pm_ContentType;
                }
                if (headstr != null)
                {
                    myReq.Headers.Add("Authorization", "Basic " + headstr);
                }
                byte[] bz = System.Text.Encoding.UTF8.GetBytes(pm_str);
                myReq.Timeout = 1800000;
                myReq.ContentLength = bz.Length;
                requestStream = myReq.GetRequestStream();
                requestStream.Write(bz, 0, bz.Length);
                requestStream.Close();
                requestStream = null;

                Response = (HttpWebResponse)myReq.GetResponse();
                reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
                string retstr = reader.ReadToEnd().Trim();
                return retstr;
            }
            catch (Exception ex)
            {
                return "{\"code\":\"0\",\"msg\":\"" + ex.Message.ToString() + "\"}";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader = null;
                }
                if (requestStream != null)
                {
                    requestStream.Close();
                    requestStream = null;
                }
                if (Response != null)
                {
                    Response.Close();
                }
            }
        }

 

你可能感兴趣的:(C# HTTP封装请求(包含Post,Get))