string data = string.Empty; System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(@"C:\Users\yafa2\Desktop\11.jpg"); using (MemoryStream ms1 = new MemoryStream()) { bmp1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] arr1 = new byte[ms1.Length]; ms1.Position = 0; ms1.Read(arr1, 0, (int)ms1.Length); ms1.Close(); data = Convert.ToBase64String(arr1); } Encoding encoding = System.Text.Encoding.Default; HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://192.9.100.36:5012/UpDownFile.asmx/UpImgByApp"); System.Text.StringBuilder builder = new System.Text.StringBuilder(); builder.AppendFormat("{0}={1}&", "data",data);//此处是图片的64位字符编码,但是会超长,请问怎么解决, builder.AppendFormat("{0}={1}&", "fileName", "110000200069_201908151740218.jpg"); builder.AppendFormat("{0}={1}", "id", "110000200069_201908151740281"); byte[] postData = System.Text.Encoding.UTF8.GetBytes(builder.ToString()); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.Timeout = 1000 * 30; req.ContentLength = postData.Length; using (System.IO.Stream reqStream = req.GetRequestStream()) { reqStream.Write(postData, 0, postData.Length); reqStream.Close(); } System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)req.GetResponse(); System.IO.Stream stream = resp.GetResponseStream(); //获取响应内容 using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8)) { data = reader.ReadToEnd(); } MessageBox.Show(data);
在post man 中没问题,为什么在c#中 就是那个data超长呢??postman用的是body方式提的,怎么用c#body提交?? 我试过了form data方式不行
你改成json格式提交看看。我记得我们做的手机端提交图片,几张手机图片可以一下提交上去。也是编码一样可以提交。就是微信接口的拍照图片格式,好像是base64编码。
教你一招一模一样的复制方式:1.开fiddler,然后raw;然后c# 把raw复制,之后用 tcp 写进入去。