/// <summary>
/// 获取Access token
/// </summary>
/// <param name="appid"></param>
/// <param name="secret"></param>
/// <returns></returns>
public static string GetAccessToken()
{
//创建一个配置
var Configuration = new ConfigurationBuilder()
//添加配置文件
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
.Build();
//赋值
_config = Configuration;
//读取key和secret
string appid = _config["WeiXingSession:AppID"];
string secret = _config["WeiXingSession:AppSecret"]; string strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret));
return GetJsonValue(strJson, "access_token");
} /// <summary>
/// HTTP请求功能
/// </summary>
internal class HttpRequestUtil
{
#region 请求Url,不发送数据
/// <summary>
/// 请求Url,不发送数据
/// </summary>
public static string RequestUrl(string url)
{
return RequestUrl(url, "POST");
}
#endregion #region 请求Url,不发送数据
/// <summary>
/// 请求Url,不发送数据
/// </summary>
public static string RequestUrl(string url, string method)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = method;
request.ContentType = "text/html";
request.Headers.Add("charset", "utf-8"); //发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}
#endregion }
public static string GetJsonValue(string jsonStr, string key)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(jsonStr))
{
key = "\"" + key.Trim('"') + "\"";
int index = jsonStr.IndexOf(key) + key.Length + 1;
if (index > key.Length + 1)
{
//先截逗号,若是最后一个,截“}”号,取最小值
int end = jsonStr.IndexOf(',', index);
if (end == -1)
{
end = jsonStr.IndexOf('}', index);
} result = jsonStr.Substring(index, end - index);
result = result.Trim(new char[] { '"', ' ', '\'' }); //过滤引号或空格
}
}
return result;
}

最新文章

  1. PHP-解析验证码类--学习笔记
  2. oracle 解锁
  3. Windows Phone 四、控件模版
  4. ASP.NET 递归将分类绑定到 TreeView
  5. 7 天玩转 ASP.NET MVC — 第 4 天
  6. ImageMagick的使用
  7. jQuery.Validate验证库
  8. 使用jvisualvm和飞行记录器分析Java程序cpu占用率过高
  9. Android Gradle Plugin指南(六)——高级构建定制
  10. cocos2dx android resources.ap_ does not exist
  11. 用es6的class关键字定义一个类
  12. 经典卷积神经网络结构——LeNet-5、AlexNet、VGG-16
  13. Codeforces.1139D.Steps to One(DP 莫比乌斯反演)
  14. [工具]K8tools github/K8工具合集/K8网盘
  15. sc (service control )
  16. CDH6离线安装
  17. github高效搜索使用总结
  18. 【转】linux下杀死进程(kill)的N种方法
  19. CF1017G The Tree 树链剖分
  20. MIT-6.828-JOS-环境搭建

热门文章

  1. P8872 [传智杯 #5 初赛] D-莲子的物理热力学
  2. viewport适配解决方案
  3. AngularJS UI
  4. C# snaps
  5. vue3.0使用富文本编辑器VueQuill
  6. H5分享功能--带图标、说明
  7. 每日一抄 Go语言死锁、活锁和饥饿概述
  8. java使用minio上传下载文件
  9. Linux 上安装 jmeter
  10. L2 Gracia Final OpCodz