#region 生成二维码
public string QR(string url = "/pages/index/index?user_id=", int type = 1)
{
var str = "";
var rst = new RestfulData<object>();
Stream s_re = WebRequest.Create("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + "" + "&secret=").GetResponse().GetResponseStream();
StreamReader s_reader = new StreamReader(s_re, Encoding.UTF8);
string[] s_out = (s_reader.ReadToEnd()).Split("{},".ToCharArray()).Distinct().ToArray();
if (s_out.Length > 0)
{
if (s_out[1].IndexOf("access_token") > 0)
{
var access_token = s_out[1].ToString().Substring(16, s_out[1].Length - 17);
str = BuildRequest(access_token, type, url);
}
}
return str;
}
public static string BuildRequest(string session, int type, string url)
{
Encoding code = Encoding.GetEncoding("utf-8");
StringBuilder sb = new StringBuilder();
sb.Append("{ \r\n");
sb.Append(" \"path\": \"" + url + "\",\r\n");//pages/details/details?id=0&tid=0&pid=0&fid="+ConfigurationSettings.AppSettings["fid"]+
sb.Append(" \"width\": \"430\"\r\n");
sb.Append("}");
//待请求参数数组字符串
string strRequestData = sb.ToString();
//把数组转换成流中所需字节数组类型
byte[] bytesRequestData = code.GetBytes(strRequestData);
//构造请求地址
string strUrl = "";
if (type == 1)
{
strUrl = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + session;
}
else
{
strUrl = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + session;
}
//请求远程HTTP
string strResult = "";
//设置HttpWebRequest基本信息
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
myReq.Method = "POST";
myReq.ContentType = "application/x-www-form-urlencoded";
//填充POST数据
myReq.ContentLength = bytesRequestData.Length;
Stream requestStream = myReq.GetRequestStream();
requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
requestStream.Close();
//发送POST数据请求服务器
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
// 把 Stream 转换成 byte[]
var time = DateTime.Now.ToString("yyyyMMdd");
string newFileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".jpg";
string Folder = System.Web.HttpContext.Current.Server.MapPath("~/Upload/img/" + time + "/");
if (!Directory.Exists(Folder))
{
Directory.CreateDirectory(Folder);
}
string filePath = Folder + newFileName;
Stream rs = HttpWResp.GetResponseStream();
System.Drawing.Image i = Image.FromStream(rs);
i.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
rs.Close();
i.Dispose();
return "https://wxapp.foodszs.cn/Upload/img/" + time + "/" + newFileName;
}
#endregion