java微信企业号40029_微信公众平台,网页授权及 40029 问题解决

  • Post author:
  • Post category:java


usingSystem;usingSystem.Web;usingNewtonsoft.Json.Linq;usingSystem.Net;usingSystem.IO;usingSystem.Text;public classgetinfos : IHttpHandler {public voidProcessRequest (HttpContext context) {//一切都从这里开始

string code = context.Request[“code”].ToString();

context.Response.ContentType= “text/plain”;

context.Response.Write(GetInfos(code));

}public boolIsReusable {get{return false;

}

}public string GetInfos(string_code)

{// string _access_tokens =GetToken(_code);

JObject json=JObject.Parse(_access_tokens);string access_token = json[“access_token”].ToString();string refresh_token = json[“refresh_token”].ToString();string openid = json[“openid”].ToString();// string _infos =GetUserInfo(access_token, openid);

JObject info=JObject.Parse(_infos);if (info[“errcode”] != null)

{

_access_tokens=RefreshToken(refresh_token);

json=JObject.Parse(_access_tokens);

access_token= json[“access_token”].ToString();

openid= json[“openid”].ToString();

_infos=GetUserInfo(access_token, openid);

}return_infos;

}///

///用code 获取 Token///

private string GetToken(string_code)

{if (_code.IndexOf(“\””) > -1)

_code= _code.Replace(“\””, “”);string URL = “https://api.weixin.qq.com/sns/oauth2/access_token”;string RequestPara = “?appid=xxx”;

RequestPara+= “&secret=xxx”;

RequestPara+= “&code=” +_code;

RequestPara+= “&grant_type=authorization_code”;string _access_tokens =GetData(URL, RequestPara);

_access_tokens= _access_tokens.Replace(“\””, “‘”);return_access_tokens;

}///

///获取用户信息///

private string GetUserInfo(string access_token, stringopenid)

{if (access_token.IndexOf(“\””) > -1)

access_token= access_token.Replace(“\””, “”);if (openid.IndexOf(“\””) > -1)

openid= openid.Replace(“\””, “”);string I_URL = “https://api.weixin.qq.com/sns/userinfo”;string I_RequestPara = “?access_token=” +access_token;

I_RequestPara+= “&openid=” +openid;

I_RequestPara+= “&lang=zh_CN”;string _infos =GetData(I_URL, I_RequestPara);

_infos= _infos.Replace(“\””, “‘”);return_infos;

}///

///刷新 Token///

private string RefreshToken(stringrefresh_token)

{if (refresh_token.IndexOf(“\””) > -1)

refresh_token= refresh_token.Replace(“\””, “”);string R_URL = “https://api.weixin.qq.com/sns/oauth2/refresh_token”;string R_RequestPara = “?appid=xxx”;

R_RequestPara+= “&grant_type=refresh_token”;

R_RequestPara+= “&refresh_token=” +refresh_token;string _infos =GetData(R_URL, R_RequestPara);

_infos= _infos.Replace(“\””, “‘”);return_infos;

}///

///访问地址,并获取数据///

private string GetData(string URL, stringRequestPara)

{

RequestPara= RequestPara.IndexOf(‘?’) > -1 ? (RequestPara) : (“?” +RequestPara);

WebRequest hr= HttpWebRequest.Create(URL +RequestPara);byte[] buf = System.Text.Encoding.GetEncoding(“utf-8”).GetBytes(RequestPara);

hr.Method= “GET”;

System.Net.WebResponse response=hr.GetResponse();

StreamReader reader= new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(“utf-8”));string ReturnVal =reader.ReadToEnd();

reader.Close();

response.Close();returnReturnVal;

}

}



版权声明:本文为weixin_35916106原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。