ASP.NET_微信JS_SDK调用

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Threading.Tasks;


namespace wechat_test
{
/// <summary>
/// test 的摘要说明
/// </summary>
public class test : IHttpHandler
{
public class tokenObj
{
public string access_token { get; set; }
public string expires_in { get; set; }
}
public class jsapiTicketObj
{
public string errcode { get; set; }
public string errmsg { get; set; }
public string ticket { get; set; }
public string expires_in { get; set; }
}

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string grant_type = "client_credential";//获取access_token填写client_credential
string AppId = "wx07b6790c0b49731a";//第三方用户唯一凭证
string secret = "8fe8a582e16436e89256f74b2b7c5275";//第三方用户唯一凭证密钥,即appsecret
//这个url链接地址和参数皆不能变
string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + AppId + "&secret=" + secret;
//string json = JsonConvert.SerializeObject(RequestUrl(url));
tokenObj t_o = JsonConvert.DeserializeObject<tokenObj>(RequestUrl(url));

//string ticket = "";

string access_token = t_o.access_token;

string url2 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi";//这个url链接和参数不能变
jsapiTicketObj j_t_o = JsonConvert.DeserializeObject<jsapiTicketObj>(RequestUrl(url2));
context.Response.Write(j_t_o.ticket);
}
#region 请求Url,不发送数据
/// <summary>
/// 请求Url,不发送数据
/// </summary>
public static string RequestUrl(string url)
{
return RequestUrl(url, "GET");
}
#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 bool IsReusable
{
get
{
return false;
}
}
}
}

ASP.NET_微信JS_SDK调用

上一篇:奇幻RPG(角色技能 与 Strategy模式)


下一篇:微信小程序快递鸟快递查询接口对接