小程序获取带参数的二维码返回结果:
{"errcode":47001,"errmsg":"data format error hint: [ougEha04061090]"}
请求URL:
Post数据:
{"access_token":"19_gWeOShK2dg9kRXRIEU3aWE2qy2SbxIvVEOEa4Hu02j8WOhr7keLVWI9CLyjluFZ1J7YJNW-c7qUchtjHlkDH2bHslXSyUo5dWbTKEXUx_iabXCn8S55s38ga1JsMQTaAEAEIL","scene":"10000","is_hyaline":"false"}
请求header:
请问怎么处理 是哪儿的错误 怎么解决
public bool GetWxaCodeUnlimit(string str, string saveName, string returnPage, out string mess, bool yaline = false, string filePath = "")
{
try {
mess = "";
var url = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={WxPayConfig.APPID}&secret={WxPayConfig.APPSECRET}";
var result = HttpService.Get(url);
var accessToken = JsonHelper.GetValue(result, "access_token");
if(accessToken == "") {
mess = "access_token获取失败";
return false;
}
// url = $"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={accessToken}";
url = $"https://api.weixin.qq.com/wxa/getwxacode?access_token={accessToken}";
var json = new JsonHelper();
json.Add("access_token", accessToken);
// json.Add("scene", str);
//json.Add("is_hyaline", yaline ? "true" : "false");
if(!returnPage.IsStringEmpty()) {
json.Add("page", returnPage);
}
var param = json.ToString().Replace("\"", "'");
var response = HttpService.PostImg(param, url, false, 10, saveName, filePath);
}
catch (Exception ex) {
Log.Error(GetType().ToString(), ex.ToString());
throw new WxPayException(ex.ToString());
}
return false;
}
public static string PostImg(string paramStr, string url, bool isUseCert, int timeout = 10, string fileName = "", string filePath = "")
{
GC.Collect(); //垃圾回收,回收没有正常关闭的http连接
var result = ""; //返回结果
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream reqStream = null;
try {
//设置最大连接数
ServicePointManager.DefaultConnectionLimit = 200;
//设置https验证方式
if(url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) {
ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
}
/***************************************************************
* 下面设置HttpWebRequest的相关属性
* ************************************************************/
request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "POST";
request.Timeout = timeout * 1000;
request.Accept = "*/*";
//设置POST的数据类型和长度
request.ContentType = "application/json;charset=UTF-8";
var data = Encoding.UTF8.GetBytes(paramStr);
request.ContentLength = data.Length;
//是否使用证书
if(isUseCert) {
var path = HttpContext.Current.Request.PhysicalApplicationPath;
var cert = new X509Certificate2(path + WxPayConfig.SSLCERT_PATH, WxPayConfig.SSLCERT_PASSWORD);
request.ClientCertificates.Add(cert);
Log.Debug("WxPayApi", "PostXml used cert");
}
//往服务器写入数据
reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
//获取服务端返回
response = (HttpWebResponse) request.GetResponse();
//获取服务端返回数据
var s = response.GetResponseStream(); //返回图片数据流
var tt = StreamToBytes(s); //将数据流转为byte[]
if(filePath.IsStringEmpty()) {
var path = "/QrCode/";
filePath = AppDomain.CurrentDomain.BaseDirectory + path;
}
if(!Directory.Exists(filePath)) {
Directory.CreateDirectory(filePath);
}
if(fileName.IsStringEmpty()) {
fileName = $"{DateTime.Now.ToString("yyyyMMddhhmmss") + ".bmp"}";
}
var name = filePath + "\\" + fileName;
File.WriteAllBytes(name, tt); //讲byte[]存储为图片
}
catch (ThreadAbortException e) {
Log.Error("HttpService", "Thread - caught ThreadAbortException - resetting.");
Log.Error("Exception message: {0}", e.Message);
Thread.ResetAbort();
}
catch (WebException e) {
Log.Error("HttpService", e.ToString());
if(e.Status == WebExceptionStatus.ProtocolError) {
Log.Error("HttpService", "StatusCode : " + ((HttpWebResponse) e.Response).StatusCode);
Log.Error("HttpService", "StatusDescription : " + ((HttpWebResponse) e.Response).StatusDescription);
}
throw new WxPayException(e.ToString());
}
catch (Exception e) {
Log.Error("HttpService", e.ToString());
throw new WxPayException(e.ToString());
}
finally {
//关闭连接和流
response?.Close();
request?.Abort();
}
return result;
}
///将数据流转为byte[]
public static byte[] StreamToBytes(Stream stream)
{
var bytes = new List<byte>();
var temp = stream.ReadByte();
while (temp != -1) {
bytes.Add((byte) temp);
temp = stream.ReadByte();
}
return bytes.ToArray();
}
post参数去掉:access_token
应该是请求写的有问题。最好贴出生成二维码的代码
已经贴了 请教下是哪儿的问题
无论用哪个,你最好把最后生成的格式打印下确认下格式是否正确
这个参数就没传递
你把最后的格式生成的格式打印看看吧。我是用PHP写的 这格式是个json字符串
{'access_token':'19__I_51HTSKmBAAw6zFlqpc8cJNGIb8k64AgqL1Qsye2XF6p0E398xlVmswASuhELd9Gg2Bf0emJfOppwmGT-lKM9ziyJpzbnyN33PkZ-xrWB2ajvp1-eppL6fYO_S7Qq6Yi5MeE1-fg_tfC5oBDCgAJAHJI'}