码迷,mamicode.com
首页 > Windows程序 > 详细

WebClient 调用api

时间:2017-06-12 17:07:37      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:web   info   ica   err   pre   exce   ade   convert   summary   

使用:
  /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="code"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static string GetUserInfo(string code, string accessToken)
        {
            string userid = "";
            string url = oApiHost + "/user/getuserinfo?access_token=" + accessToken + "&code=" + code;
            string result = Tool.HttpGet(url);

            dynamic data = JsonConvert.DeserializeObject<dynamic>(result);


            if (data.errcode == 0)
            {
                userid = data.userid;
            }
            else
            {
                throw new Exception("获取User Id发生错误,钉钉返回:" + Convert.ToString(data.errmsg));
            }
            return userid;
        }

 

Tool类:

 public class Tool
    {
        public static string HttpGet(string url)
        {
            WebClient client = new WebClient();
            client.Headers.Add("Content-Type", "application/json");
            client.Encoding = Encoding.UTF8;
            string result = client.DownloadString(url);
            return result;
        }

        public static string HttpPost(string url, NameValueCollection data)
        {
            WebClient client = new WebClient();
            client.Headers.Add("Content-Type", "application/json");
            client.Encoding = Encoding.UTF8;
            byte[] origin_result = client.UploadValues(url, data);
            return Encoding.UTF8.GetString(origin_result);
        }
}

 

 

WebClient 调用api

标签:web   info   ica   err   pre   exce   ade   convert   summary   

原文地址:http://www.cnblogs.com/xuguanghui/p/6993625.html

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