WebClient 调用api
Posted hope
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebClient 调用api相关的知识,希望对你有一定的参考价值。
使用:
/// <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的主要内容,如果未能解决你的问题,请参考以下文章
onActivityResult 未在 Android API 23 的片段上调用