//获取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());
}
}
请发表评论