生成的小程序二维码图片无法打开?
发布于 6 年前 作者 jiekong 4663 次浏览 来自 官方Issues

最近发现通过小程序接口返回的二维码保存后,却无法打开。

前段时间还可以的,不知道是微信改了什么吗?

请帮忙看看。

下面是代码

public void CreateQrCode()
{
    var service = new CommonService();
    var token = service.GetAccessToken();
    var url = $"https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={token}";
    var qrcodeContent = Guid.NewGuid().ToString();
    var body = $"{{\"path\":\"pages/xxx?q='{qrcodeContent}'\"}}";
 
    var results = HttpPost(url, body);
 
    ToImage(results);
}
 
public Stream HttpPost(string url, string body)
{
    Encoding encoding = Encoding.UTF8;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    //request.ContentType = "application/json; charset=utf-8";
 
    byte[] buffer = encoding.GetBytes(body);
    request.ContentLength = buffer.Length;
    var witer = request.GetRequestStream();
    witer.Write(buffer, 0, buffer.Length);
    witer.Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 
    return response.GetResponseStream();
}
 
private static void ToImage(Stream fileSteam)
{
    var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
        "test2.jpg");
 
    byte[] bytes;
    using (MemoryStream ms = new MemoryStream())
    {
        fileSteam.CopyTo(ms);
        bytes = ms.ToArray();
    }
    using (var fs = File.OpenWrite(path))
    {
        fs.Write(bytes, 0, bytes.Length);
    }
 
    Process.Start(path);
}
回到顶部