Service-Terminal
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Script.Serialization; using System.Web.Script.Services; using System.Runtime.Serialization.Json; using System.Data; using System.Text; using System.IO; using System.Net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; /// <summary> /// api 的摘要说明 /// </summary> [WebService (the Namespace = " http://tempuri.org/ " )] [WebServiceBinding (conformsTo = WsiProfiles.BasicProfile1_1)] // To call this allows the use of ASP.NET AJAX Web service from a script, uncomment the following line . [System.Web.Script.Services.ScriptService] public class API: the System.Web.Services.WebService { [the WebMethod] public void the OpenID ( String code) { // temporary login credentials session_key code acquisition and OpenID String js_code = code; / / here to fill out their own secret appid and applets String serviceAddress = "https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code=" + js_code + "&grant_type=authorization_code"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); request.Method = "GET"; request.ContentType = "textml;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = New new the StreamReader (myResponseStream, Encoding.UTF8); String jsonData = myStreamReader.ReadToEnd (); myStreamReader.Close (); myResponseStream.Close (); String JSONString = jsonData; a JObject JSON = JObject.Parse (JSONString); String OpenID = JSON [ " OpenID " ] .ToString (); context.Response.Write (getJSON (OpenID)); } #region the target sequence JSON string /// <Summary> /// the target sequence JSON string / // </ Summary> /// <name = "T" typeparam> Object Type </ typeParam> /// <param name = "obj"> object entity </ param> /// <Returns> the JSON string </ Returns> public static String getJSON <T > (T obj) { // Remember to add a reference System.ServiceModel.Web / * * * if you do not add the above references, System.Runtime.Serialization.Json; Json is not right Oh * * / the DataContractJsonSerializer JSON = new new the DataContractJsonSerializer ( typeof (T)); the using (MS = the MemoryStream new new the MemoryStream ()) { json.WriteObject(ms, obj); string szJson = Encoding.UTF8.GetString(ms.ToArray()); return szJson; } } #endregion }