易班2016.net SDK RestSharp access_token获取错误 解决方案

自己重新写一个POST方式函数

        public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, Encoding charset)
        {
            HttpWebRequest request = null;
            request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";//官网要求使用from-data方式POST,很迷,实际上用这种也能取到
            //如果需要POST数据     
            if (!(parameters == null || parameters.Count == 0))
            {
                StringBuilder buffer = new StringBuilder();
                int i = 0;
                foreach (string key in parameters.Keys)
                {
                    if (i > 0)
                    {
                        buffer.AppendFormat("&{0}={1}", key, parameters[key]);
                    }
                    else
                    {
                        buffer.AppendFormat("{0}={1}", key, parameters[key]);
                    }
                    i++;
                }
                byte[] data = charset.GetBytes(buffer.ToString());
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            return request.GetResponse() as HttpWebResponse;
        }

            //此处为调用函数截取
            string url = "https://openapi.yiban.cn/oauth/access_token";;
            Encoding encoding = Encoding.GetEncoding("utf-8");
            IDictionary<string, string> parameters = new Dictionary<string, string>
            {
                { "client_id", context.Config.AppId },
                { "client_secret", context.Config.AppSecret },
                { "code", code },
                { "redirect_uri", context.Config.Callback }
            };
            HttpWebResponse response1 = CreatePostHttpResponse(url, parameters, encoding);
            //打印返回值  
            Stream stream = response1.GetResponseStream();   //获取响应的字符串流  
            StreamReader sr = new StreamReader(stream); //创建一个stream读取流  
            string html = sr.ReadToEnd();   //从头读到尾  
            var result1 = Deserialize<AccessToken>(html);//能获取到,之后就用SDK封装好的api

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页