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

http

时间:2019-01-17 19:44:33      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:客户端   method   ext   表示   reg   style   string   author   ESS   

 #region 获取wishpost的token和refresh_token
        /// <summary>
        /// 获取wishpost的token和refresh_token
        /// </summary>
        /// <param name="code">授权码</param>
        /// <returns>返回收到的结果</returns>
        public string wishpost_gettoken(string code)
        {
            string htmlStr = string.Empty;

            try
            {
                string url = "https://wishpost.wish.com/api/v3/access_token";
                string authorization_str = "Basic {123123123123123}";
                string redirect_uri = "https://wishpost.corp.contextlogic.com/"; //表示重定向URI
                string client_id = "12345678"; //表示客户端ID

                string strPostdata = "grant_type=authorization_code&redirect_uri=" + redirect_uri + "&client_id=" + client_id + "&code=" + code;
                Encoding encoding = Encoding.UTF8;


                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
                request.Headers.Add("Authorization", authorization_str);
                request.AllowAutoRedirect = true;
                request.Method = "POST";


                byte[] buffer = encoding.GetBytes(strPostdata);

                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);

                //获取当前Http请求的响应实例
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream responseStream = response.GetResponseStream();

                using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
                {
                    htmlStr = reader.ReadToEnd();
                }

                responseStream.Close();
            }
            catch (WebException ex)
            {

                HttpWebResponse res = (HttpWebResponse)ex.Response;
                StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
                htmlStr = sr.ReadToEnd();
                sr.Close();
            }
            return htmlStr;
            
        }
        #endregion

 

http

标签:客户端   method   ext   表示   reg   style   string   author   ESS   

原文地址:https://www.cnblogs.com/niyl/p/10283476.html

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