public HttpResponseMessage GetQRCode() { string token = GetAccessToken(sAppID); string apiUri = string.Format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}", token); HttpClient client = new HttpClient(); string requestJson = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 1234567890}}}"; StringContent content = new StringContent(requestJson, Encoding.UTF8); var response = client.PostAsync(apiUri, content); string tickJson = response.Result.Content.ReadAsStringAsync().Result; AccessTokenTicket ticket = Newtonsoft.Json.JsonConvert.DeserializeObject <AccessTokenTicket>(tickJson); apiUri = string.Format("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}", ticket.ticket); response = client.GetAsync(apiUri); return(response.Result); }
public HttpResponseMessage GetQRCodeLimit(string sceneStr) { SQLHelper db = new SQLHelper(connString); byte[] qrData = db.GetSceneQR(sceneStr); if (qrData == null) { string token = GetAccessToken(sAppID); string apiUri = string.Format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}", token); HttpClient client = new HttpClient(); string requestJson = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + sceneStr + "\"}}}"; StringContent content = new StringContent(requestJson, Encoding.UTF8); var response = client.PostAsync(apiUri, content); string tickJson = response.Result.Content.ReadAsStringAsync().Result; AccessTokenTicket ticket = Newtonsoft.Json.JsonConvert.DeserializeObject <AccessTokenTicket>(tickJson); apiUri = string.Format("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}", ticket.ticket); response = client.GetAsync(apiUri); db.InsertSceneQR(sceneStr, sAppID, response.Result.Content.ReadAsByteArrayAsync().Result); return(response.Result); } else { HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.OK); //ObjectContent content = new ObjectContent( ByteArrayContent cont = new ByteArrayContent(qrData); MemoryStream ms = new MemoryStream(qrData, 0, qrData.Length); StreamContent stream = new StreamContent(ms); msg.Content = stream; HttpContext.Current.Response.ContentType = "image/jpeg"; //Request.CreateResponse<byte[]>(HttpStatusCode.OK, qrData, "text/html"); return(msg); } }