以前用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;
}
v3交换传输用的是`json`, 你的`StringBuilder`代码没用了,另外没有做 `Authorization` 头认证值签名,返回 401 是预期结果。