string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "/temp/", filename);
var sha256 = Applyments.SHA256File(filePath);
meta meta = new meta()
{
sha256 = sha256,
filename = System.IO.Path.GetFileName(filePath)
};
var json = JsonConvert.SerializeObject(meta);
string mchid = site.WechatMchid;
string serialNo = site.WechatSerialNo;
var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
string nonce = Guid.NewGuid().ToString("n");
string method = "POST";
string uri = "/v3/merchant/media/upload";
string body = json;
string message = $"{method}\n{uri}\n{timestamp}\n{nonce}\n{body}\n";
string signature = Applyments.Sign(message, mchid);
string auth = $"mchid=\"{mchid}\",nonce_str=\"{nonce}\",timestamp=\"{timestamp}\",serial_no=\"{serialNo}\",signature=\"{signature}\"";
string value = $"WECHATPAY2-SHA256-RSA2048 {auth}";
// 定义数据分隔线
string boundary = string.Format("{0}", DateTime.Now.Ticks.ToString("x"));
string Url = "https://api.mch.weixin.qq.com/v3/merchant/media/upload";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.Headers.Add(HttpRequestHeader.Authorization, value);
//request.Accept = "application/json";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36";
request.ContentType = "multipart/form-data;boundary=" + boundary;
// 换行符
string LINE_END = "\r\n";
string PREFIX = "--";
//拼装请求内容第一部分
StringBuilder strSb = new StringBuilder();
StringBuilder strSb1 = new StringBuilder();
strSb1.Append(PREFIX).Append(boundary);
strSb.Append(PREFIX).Append(boundary).Append(LINE_END)
.Append("Content-Disposition: form-data; name=\"meta\";" + LINE_END)
.Append("Content-Type: application/json; " + LINE_END)
.Append(LINE_END)// 空行
.Append("{\"filename\":\"" + meta.filename + "\",\"sha256\":\"" + meta.sha256 + "\"}")
.Append(LINE_END);
//拼装请求内容第二部分
strSb.Append(PREFIX).Append(boundary).Append(LINE_END)
.Append("Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\";" + LINE_END)
.Append("Content-Type: image/jpeg" + LINE_END)
.Append(LINE_END);// 空行
//拼接图片文件流
FileStream fs = new FileStream(filePath, FileMode.Open);//可以是其他重载方法
byte[] byData = new byte[fs.Length];
fs.Read(byData, 0, byData.Length);
fs.Close();
string stringFiles = Convert.ToBase64String(byData);
strSb.Append(stringFiles).Append(LINE_END);
//结尾
strSb.Append(PREFIX).Append(boundary).Append(PREFIX)
.Append(LINE_END);
byte[] data = Encoding.UTF8.GetBytes(strSb.ToString());
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(data, 0, data.Length);
// 获取二进制数据
byte[] postBytes = memoryStream.ToArray();
request.ContentLength = postBytes.Length;
using(Stream reqStream = request.GetRequestStream())
{
reqStream.Write(postBytes, 0, postBytes.Length);
reqStream.Close();
}
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
//获取响应内容
StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
reader.Close();
您好 请问 您的400服务器错误是怎么解决的 能分享下吗
你好,你这边调用的是哪个图片上传接口?麻烦详细描述下,图片上传有营销专用接口,还有进件接口。
【文档问题您来提,精美好礼我来送~微信支付文档期待您的声音~】欢迎点击下方链接了解微信支付文档有奖反馈活动详情:https://developers.weixin.qq.com/community/pay/doc/000aa24226cb18f5cc1ba2b815b001
文档反馈入口:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/api.shtml