收藏
回答

V3 c#的JSAPI下单接口post老是出问题,麻烦指点一下

    以前用V2,返回错误是401,不知道哪里出了问题

{ "mchid": "142888888", "out_trade_no": "142579670220210628092942740", "notify_url": "URL", "amount": { "total": 1, "urrency": "CNY" }, "payer": { "openid": "oYMAs5EuWA4r_MvX0UC2M_ku6YBw" } }


  public static string Post(string url, JObject dic)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "POST";
        req.AutomaticDecompression = DecompressionMethods.GZip;
        req.ContentType = "application/json";
        req.Accept = "application/json";
        req.Referer =url;
        req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586";
        StringBuilder builder = new StringBuilder();
        int i = 0;
        if (dic.Count > 0)
        {
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
        }
        byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
        req.ContentLength = data.Length;
        using (Stream reqStream = req.GetRequestStream())
        {
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
        }
        string result = "";
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        Stream stream = resp.GetResponseStream();
        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
        {
            result = reader.ReadToEnd();
        }
        return result;
    }
回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容