编程语言
首页 > 编程语言> > ASP.NET WEBAPI 获取微信ticket

ASP.NET WEBAPI 获取微信ticket

作者:互联网

public static string GetTicket(string AccessToken)
{
//类型
string Type = "jsapi";
string strurl = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type={1}", AccessToken, Type);

var wc = new WebClient();
//取得微信返回的json数据
var strReturn = wc.DownloadString(strurl);
string ticket = GetJsonValue(strReturn, "ticket");
ticket = ticket.Replace("\\", "");
return ticket;

}

 

/// <summary>
/// 获取Json字符串某节点的值
/// </summary>
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;
}

标签:WEBAPI,index,ASP,string,jsonStr,微信,result,key,ticket
来源: https://www.cnblogs.com/2018cjx/p/15065528.html