收藏
回答

C#调用微信公众号创建自定义菜单接口一直报47001?

C#调用微信公众号创建自定义菜单接口一直报47001,格式是正确的,就是复制官网的示例格式,用官方接口测试工具也通过,用postman也通过,但就是C#后台调用接口报47001。

菜单是用json文件存储的,文件的编码是UTF8。

{
  "button": [
    {
      "type": "click",
      "name": "今日歌曲",
      "key": "V1001_TODAY_MUSIC"
    },
    {
      "name": "菜单",
      "sub_button": [
        {
          "type": "view",
          "name": "搜索",
          "url": "http://www.soso.com/"
        },
        {
          "type": "click",
          "name": "赞一下我们",
          "key": "V1001_GOOD"
        }
      ]
    }
  ]
}

读取菜单json文件也是用UTF8读取出来,调试的时候看读取的字符串中文是正常的。

/// <summary>
/// 创建自定义菜单
/// </summary>
private void CreateMenu()
{
    try
    {
        // 获取当前项目的根目录
        string root = HostingEnvironment.MapPath("~/");
        FileStream fs1 = new FileStream(root + "\\menu.json", FileMode.Open);
        StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("UTF-8"));
        string menu = sr.ReadToEnd();
        sr.Close();
        fs1.Close();

        // 获取 assess_token
        string assessToken = GetAccessToken();
        // 接口地址
        string interfaceUrl = $"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={assessToken}";
        // 发送请求
        SendWxRequest(interfaceUrl, menu);
    }
    catch (Exception ex)
    {
        LogHelper.Default.WriteError(ex.Message, ex);
    }
}


/// <summary>
/// 发送微信接口的请求
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <returns></returns>
private void SendWxRequest(string url, string data)
{
    Encoding encoding = Encoding.UTF8;
    byte[] dataBytes = encoding.GetBytes(data);
    // 准备请求...
    try
    {
        // 设置参数
        HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
        CookieContainer cookieContainer = new CookieContainer();
        webRequest.CookieContainer = cookieContainer;
        webRequest.AllowAutoRedirect = true;
        webRequest.Method = "POST";
        // application/json
        // application/x-www-form-urlencoded
        webRequest.ContentType = "application/json";
        webRequest.ContentLength = data.Length;
        Stream outstream = webRequest.GetRequestStream();
        outstream.Write(dataBytes, 0, data.Length);
        outstream.Close();

        // 发送请求并获取相应回应数据
        HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
        // 直到 request.GetResponse() 程序才开始向目标网页发送 Post 请求
        Stream instream = webResponse.GetResponseStream();
        StreamReader sr = new StreamReader(instream, encoding);
        // 返回结果网页(html)代码
        string result = sr.ReadToEnd();

        // 将结果转换为实体类
        WxResultDto wxResultDto = JsonConvert.DeserializeObject<WxResultDto>(result);
        if (wxResultDto?.Errcode == 0)
        {
            Debug.WriteLine($"接口 {url} 请求成功");
        }
        else
        {
            Debug.WriteLine($"接口 {url} 请求出现错误,错误消息:{wxResultDto.Errmsg}");
        }
        LogHelper.Default.WriteInfo($"请求地址:{url},返回结果:{result}");
    }
    catch (Exception ex)
    {
        throw ex;
    }
}


都统一用的UTF8编码格式,结果还是报47001.

我再把menu.json里中文全部改成英文,就正常了,求助!!


回答关注问题邀请回答
收藏
登录 后发表内容
问题标签