亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# wx獲取token的基本方法

 更新時(shí)間:2017年06月19日 09:01:42   作者:猿大頭  
這篇文章主要為大家詳細(xì)介紹了C# wx獲取token的基本方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C# wx獲取token的具體代碼,供大家參考,具體內(nèi)容如下

#region 請(qǐng)求Url,不發(fā)送數(shù)據(jù)

/// <summary>
/// 請(qǐng)求Url,不發(fā)送數(shù)據(jù)
/// </summary>
public static string RequestUrl(string url)
{
return RequestUrl(url, "POST");
}

#endregion

#region 請(qǐng)求Url,不發(fā)送數(shù)據(jù)

/// <summary>
/// 請(qǐng)求Url,不發(fā)送數(shù)據(jù)
/// </summary>
public static string RequestUrl(string url, string method)
{
// 設(shè)置參數(shù)
var request = WebRequest.Create(url) as HttpWebRequest;
var cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = method;
request.ContentType = "text/html";
request.Headers.Add("charset", "utf-8");
//發(fā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù)
var response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才開(kāi)始向目標(biāo)網(wǎng)頁(yè)發(fā)送Post請(qǐng)求
Stream responseStream = response.GetResponseStream();
var sr = new StreamReader(responseStream, Encoding.UTF8);
//返回結(jié)果網(wǎng)頁(yè)(html)代碼
string content = sr.ReadToEnd();

return content;
}

#endregion

 

#region 獲取Json字符串某節(jié)點(diǎn)的值

/// <summary>
/// 獲取Json字符串某節(jié)點(diǎn)的值
/// </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)
{
//先截逗號(hào),若是最后一個(gè),截"}"號(hào),取最小值
int end = jsonStr.IndexOf(',', index);
if (end == -1)
{
end = jsonStr.IndexOf('}', index);
}
result = jsonStr.Substring(index, end - index);
result = result.Trim(new [] {'"', ' ', '\"'}); //過(guò)濾引號(hào)或空格
}
}
return result;
}

#endregion

#region 驗(yàn)證Token是否過(guò)期

/// <summary>
/// 驗(yàn)證Token是否過(guò)期
/// </summary>
public static bool TokenExpired(string access_token)
{
string jsonStr =
RequestUrl(string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}",
access_token));
if (GetJsonValue(jsonStr, "errcode") == "42001")
{
return true;
}
return false;
}

#endregion

#region 獲取Token

/// <summary>
/// 獲取Token
/// </summary>
public static string GetToken(string appid, string secret)
{
string strJson =
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");
}

#endregion

//獲取Openid

public static string GetOpenId(string appid, string secret, string code)
{
string strJson =
RequestUrl(
string.Format(
"https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code",
appid, secret, code));
//LogUtil.WriteLog(strJson);
return GetJsonValue(strJson, "openid");
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C# TreeView控件使用代碼

    C# TreeView控件使用代碼

    TreeView控件的實(shí)例代碼,需要的朋友可以參考下。
    2009-09-09
  • DataGridView帶圖標(biāo)的單元格實(shí)現(xiàn)代碼

    DataGridView帶圖標(biāo)的單元格實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了DataGridView帶圖標(biāo)的單元格的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Unity實(shí)現(xiàn)UI漸隱漸顯效果

    Unity實(shí)現(xiàn)UI漸隱漸顯效果

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)UI漸隱漸顯效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#實(shí)現(xiàn)Winform版計(jì)算器

    C#實(shí)現(xiàn)Winform版計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)Winform版計(jì)算器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • C#實(shí)現(xiàn)計(jì)算器功能

    C#實(shí)現(xiàn)計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET

    直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET

    這篇文章主要用asp.net技術(shù)實(shí)現(xiàn)直接在線預(yù)覽word、excel、txt文件,有需要的朋友可以參考下
    2015-08-08
  • C#實(shí)現(xiàn)的SQL備份與還原功能示例

    C#實(shí)現(xiàn)的SQL備份與還原功能示例

    這篇文章主要介紹了C#實(shí)現(xiàn)的SQL備份與還原功能,結(jié)合具體實(shí)例形式分析了C#操作數(shù)據(jù)庫(kù)實(shí)現(xiàn)SQL備份與還原相關(guān)的控件、SQL連接、文件等操作技巧,需要的朋友可以參考下
    2017-06-06
  • C#中System.Text.Json匿名對(duì)象反序列化

    C#中System.Text.Json匿名對(duì)象反序列化

    這篇文章主要介紹了System.Text.Json匿名對(duì)象反序列化,下文代碼基于. NET 6,為了代碼整潔,實(shí)際配置了PropertyNameCaseInsensitive = true,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • C#結(jié)合JavaScript實(shí)現(xiàn)手寫(xiě)板簽名效果

    C#結(jié)合JavaScript實(shí)現(xiàn)手寫(xiě)板簽名效果

    這篇文章主要為大家詳細(xì)介紹了C#如何結(jié)合JavaScript實(shí)現(xiàn)手寫(xiě)板寫(xiě)字并上傳到服務(wù)器進(jìn)行處理,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • C#中IEnumerable、ICollection、IList、List之間的區(qū)別

    C#中IEnumerable、ICollection、IList、List之間的區(qū)別

    IEnumerable、ICollection、IList、List之間的區(qū)別,本文分別分析了它的實(shí)現(xiàn)源碼,從而總結(jié)出了它們之間的關(guān)系和不同之處。對(duì)C# IEnumerable、ICollection、IList、List相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-07-07

最新評(píng)論