• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

小程序二维码生成

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

//获取access_token

public static string HttpGet(string Url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}

//获取二维码保存到本地

[HttpGet]
public ActionResult PostMoths(string shareUserGuid)
{


string info = "";
string imgName = shareUserGuid + ".jpg";
//文件存储相对于当前应用目录的虚拟目录
string path = "/image/";
//获取相对于应用的基目录,创建目录
string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + path; //通过此对象获取文件名
if (!Directory.Exists(path + imgName))
{

string result = HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WxPayConfig.wxappid + "&secret=" + WxPayConfig.wxsecret);
accessTokenResult accessTokenResult = new accessTokenResult();
var obj = JsonParser.JsonToObject(result, accessTokenResult);
accessTokenResult obj2 = (accessTokenResult)obj;
string _url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + obj2.access_token;
string strURL = _url;
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
JsonData _data = new JsonData();
_data.path = "/pages/register/register?Shareuserguid=" + shareUserGuid;
_data.width = "430";
string _jso = JsonParser.ObjectToJson(_data);
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(_jso);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
byte[] tt = StreamToBytes(s);

//string imgName = shareUserGuid+ ".jpg";
////文件存储相对于当前应用目录的虚拟目录
//string path = "/image/";
////获取相对于应用的基目录,创建目录
//string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + path; //通过此对象获取文件名
if (!Directory.Exists(imgPath))
{
Directory.CreateDirectory(imgPath);
}
System.IO.File.WriteAllBytes(HttpContext.Server.MapPath(path + imgName), tt);//讲byte[]存储为图片
info = "/image/" + imgName;
}
else
{
info = "/image/" + imgName;

}
json = ReturnJsonInfo(info.ToString());
return Content(json);

}


///将数据流转为byte[]
public static byte[] StreamToBytes(Stream stream)
{
List<byte> bytes = new List<byte>();
int temp = stream.ReadByte();
while (temp != -1)
{
bytes.Add((byte)temp);
temp = stream.ReadByte();
}
return bytes.ToArray();
}

 


/// <summary>
/// 拼接JSON结构
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public string ReturnJsonInfo(string info)
{

return "{\"status\":\"" + code + "\",\"error\":\"" + error + "\",\"msg\":null,\"encrypted\":true,\"message\":\"" + info + "\"}";

}

 

//定义接收的类

public class JsonData
{
public string path { get; set; }
public string width { get; set; }

}
public class accessTokenResult
{
public string access_token { get; set; }
public string expires_in { get; set; }

}

 

public static class JsonParser
{

/// <summary>
/// // 从一个对象信息生成Json串
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ObjectToJson(object obj)
{
return JsonConvert.SerializeObject(obj);

}
/// <summary>
///从一个Json串生成对象信息
/// </summary>
/// <param name="jsonString"></param>
/// <param name="obj"></param>
/// <returns></returns>

public static object JsonToObject(string jsonString, object obj)
{
return JsonConvert.DeserializeObject(jsonString, obj.GetType());
}

}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap