MVC 微信开发获取用户OpenID
2017-08-11 15:53
309 查看
1 public class HttpQuery { 2 private static readonly string DefaultUserAgent = 3 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; 4 5 public static void Get(string url, object data, Action<string> callback) { 6 IDictionary<string, string> parameters = Getparameters(data); 7 8 if (!(parameters == null || parameters.Count == 0)) { 9 url += "?"; 10 foreach (var item in parameters) { 11 url += item.Key + "=" + item.Value + "&"; 12 } 13 } 14 CreateGetHttpResponse(url, null, null, null, callback); 15 } 16 /// <summary> 17 /// 创建GET方式的HTTP请求 18 /// </summary> 19 /// <param name="url">请求的URL</param> 20 /// <param name="timeout">请求的超时时间</param> 21 /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> 22 /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> 23 /// <returns></returns> 24 private static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, 25 CookieCollection cookies, Action<string> callback, string encoding = "utf-8") { 26 if (string.IsNullOrEmpty(url)) { 27 return null; 28 //throw new ArgumentNullException("url"); 29 } 30 try { 31 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 32 request.Method = "GET"; 33 request.UserAgent = DefaultUserAgent; 34 if (!string.IsNullOrEmpty(userAgent)) { 35 request.UserAgent = userAgent; 36 } 37 if (timeout.HasValue) { 38 request.Timeout = timeout.Value; 39 } 40 if (cookies != null) { 41 request.CookieContainer = new CookieContainer(); 42 request.CookieContainer.Add(cookies); 43 } 44 45 HttpWebResponse httpWebResponse = request.GetResponse() as HttpWebResponse; 46 47 StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), 48 System.Text.Encoding.GetEncoding(encoding)); 49 50 string html = ""; 51 //获取请求到的数据 52 html = reader.ReadToEnd(); 53 //关闭 54 httpWebResponse.Close(); 55 reader.Close(); 56 57 callback(html); 58 return httpWebResponse; 59 } 60 } catch { 61 callback(null); 62 } 63 return null; 64 } 65 66 }
Http GET请求
相关文章推荐
- MVC 微信开发获取用户OpenID
- 微信小程序开发(四)获取用户openid
- 微信小程序开发之获取openid及用户信息
- 微信小程序开发之获取openid及用户信息
- 微信测试号开发之九 微信网页授权:页面获取用户openid
- 微信测试号开发之九 微信网页授权:页面获取用户openid
- [置顶] java微信开发-之如何获取openid 和用户信息
- 微信公众号开发-微信网页授权获取用户openid以及用户信息的步骤
- 微信公众号开发之获取微信用户的openID
- 微信公众号开发-微信网页授权获取用户openid以及用户信息的步骤
- 微信开发---------------------------网页授权获取用户openId
- .NET微信开发通过Access Token和OpenID获取用户信息
- php之微信开发获取用户openid的方法
- 微信开发笔记:获取用户openid,以及用户头像昵称等信息
- C#微信开发之旅(八):通过授权code以及openid获取用户信息
- php CI框架微信开发小记——获取openid和其他相关用户信息
- [004]微信公众账号开发教程第1篇-根据用户openID获取用户基本信息
- 微信平台开发获取微信用户的OPENID
- 微信小程序开发之用户系统 一键登录 获取session_key和openid
- 微信小程序开发之用户系统 一键登录 获取session_key和openid