码迷,mamicode.com
首页 > Web开发 > 详细

HTTP请求方法

时间:2018-01-25 16:47:53      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:har   eth   ret   set   headers   creat   data   tco   ==   

 #region HTTP请求方法
        public string HttpPost(string Url, string postDataStr = "", string access_token = "")
        {

            //CookieContainer cookie = new CookieContainer();


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            if (!string.IsNullOrWhiteSpace(access_token))
            {

                request.Headers.Add("Authorization", access_token);
            }
            request.Method = "POST";
            request.ContentType = "application/json;charset=utf-8";

            if (!string.IsNullOrWhiteSpace(postDataStr))
            {
                request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            }

            //request.CookieContainer = cookie;
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            //StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8"));
            if (!string.IsNullOrWhiteSpace(postDataStr))
            {
                myStreamWriter.Write(postDataStr);
            }
            myStreamWriter.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            //response.Cookies = cookie.GetCookies(response.ResponseUri);
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

        public string HttpGet(string Url, string postDataStr = "", string access_token = "")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            if (!string.IsNullOrWhiteSpace(access_token))
            {

                request.Headers.Add("Authorization", access_token);
            }

            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;
        }

        #endregion

 

HTTP请求方法

标签:har   eth   ret   set   headers   creat   data   tco   ==   

原文地址:https://www.cnblogs.com/zyx321/p/8351681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!