C# winform 向服务器Post请求上传图片 图片以byte[ ] 的流的形式传入

Zoe_77 2016-07-08 11:49:02
这个是我写的post请求,接口没问题,数据格式也对,但是就是得不到响应,请求的时候服务器端没有响应

/// <summary>
/// 向服务器Post请求上传图片
/// </summary>
/// <param name="upload_url">请求地址 http://192.168.0.666:8080/aaa/uploadByPc.do </param>
/// <param name="id">项目索引</param>
/// <param name="images">原图byte[]</param>
/// <param name="thumbs">缩略图byte[]</param>
/// <param name="macCode">初次上传索引</param>
public static void HttpPost(string upload_url, long id, byte[] images, byte[] thumbs, long macCode)//Post请求
{
//构造post提交字段
string para = "";
if (macCode != 0)
{
para = string.Format("id={0}&images={1}&thumbs={2}", id, images, thumbs);
}
else
{
para = string.Format("id={0}&images={1}&thumbs={2}&macCode={3}", id, images, thumbs, macCode);
}
try
{
HttpWebRequest httpWeb = (HttpWebRequest)WebRequest.Create(upload_url);
byte[] bytePara = Encoding.UTF8.GetBytes(para);
httpWeb.Method = "POST";
httpWeb.Timeout = 30000;
httpWeb.ContentType = "application/x-www-form-urlencoded";
//application/x-www-form-urlencoded////multipart/form-data
httpWeb.ContentLength = bytePara.Length;
using (Stream reqStream = httpWeb.GetRequestStream())
{
//提交数据
reqStream.Write(bytePara, 0, para.Length);
}

////获取服务器返回值
//HttpWebResponse httpWebResponse = (HttpWebResponse)httpWeb.GetResponse();
//Stream stream = httpWebResponse.GetResponseStream();
//StreamReader streamReader = new StreamReader(stream, Encoding.Default);
////获得返回值
//string result = streamReader.ReadToEnd();
//stream.Close();

//将服务器返回值返回
//return result;
}catch(Exception ex){
//return ex.ToString();
}
...全文
2720 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
一aa一 2016-07-10
  • 打赏
  • 举报
回复
图片直接从表单里面传WEB服务器不可以么???
wanghui0380 2016-07-08
  • 打赏
  • 举报
回复
给你个例子,稍微改一下就可以了
    private ByteArrayContent CreateFileContent(System.IO.MemoryStream stream, string fileName, string contentType)
        {
            var fileContent = new ByteArrayContent(stream.ToArray());
            fileContent.Headers.Clear();
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "\"file\"",
                FileName = "\"" + fileName + "\""
            }; // the extra quotes are key here
            fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            return fileContent;
        }

 using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
//我这里接收的是stream,不过你传byte[],那就自己改一下
                bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                HttpClient client = new HttpClient();

                MultipartFormDataContent form = new MultipartFormDataContent();
                form.Add(CreateFileContent(ms, msg.ToString() + ".jpg", "application/octet-stream"));
                
                
                form.Add(“其他附属字段key”, '"' + "其他附属字段的value" + '"');

          
                var result = client.PostAsync(url, form).Result.Content.ReadAsStringAsync().Result;
            }
  • 打赏
  • 举报
回复
既然键值对的形式上传,那为啥还要byte[]呢?直接base64字符串形式上传不就可以了
Zoe_77 2016-07-08
  • 打赏
  • 举报
回复
自己先顶一个
  • 打赏
  • 举报
回复
像代码
para = string.Format("id={0}&images={1}&thumbs={2}", id, images, thumbs);
这种东西,你稍微调试一下 para 变量的值,就知道有什么问题了。 看来你从来不调试。
  • 打赏
  • 举报
回复
先服务器上传文件,写一句
new WebClient().UploadFile(url, file);
就可以了。 你写那么多,何必呢?
带头大哥_ 2016-07-08
  • 打赏
  • 举报
回复
你这肯定不行 1.有文件的话ContentType必须是multipart/form-data 2.你这个字节也不对 你可以写个html post提交后用浏览器看请求流里面的数据就知道了,ContentType是multipart/form-data的时候需要涉及到分割,提交过去的数据应该是类似下面着这样 -----------------------------7da32c172e0acc Content-Disposition: form-data; name="litpic"; filename="" Content-Type:image/png -----------------------------7da32c172e0acc Content-Disposition: form-data; name="nativeplace"

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧