c#調用微信接口獲取token值

廢話不說,一個方法實現,這裏的url 參數即爲配置好的訪問路徑,html

string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + AppID + "&secret=" + AppSecret;  api

這裏的兩個參數AppID、AppSecret對應微信公衆帳號微信

public static string GetPage(string url )
{
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;cookie

// 準備請求...
try
{
// 設置參數
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
//發送請求並獲取相應迴應數據
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序纔開始向目標網頁發送Post請求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回結果網頁(html)代碼
string content = sr.ReadToEnd();
Logs.WriteLog("" + DateTime.Now + content);  //記錄返回結果
return content;
}
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
}app

相關文章
相關標籤/搜索