温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# form-data上传图片流到远程服务器怎么实现

发布时间:2022-08-30 15:14:42 来源:亿速云 阅读:140 作者:iii 栏目:开发技术

这篇文章主要介绍“C# form-data上传图片流到远程服务器怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C# form-data上传图片流到远程服务器怎么实现”文章能帮助大家解决问题。

先贴代码,后面做一些简单说明:

public static string sendPostHttpRequest_2(string url, byte[] postBytes, string contentType= "multipart/form-data; boundary=--------------------------71b23e4066ed")
        {
            string delimiter = "--------------------------71b23e4066ed";
            string eol = Environment.NewLine;
            string head = delimiter + eol
        + "Content-Disposition: form-data;piclen=" + postBytes.Length + eol
       + "Content-Type:image/jpeg" + "\r\n\r\n";
            string foot = "\r\n" + delimiter + "--\r\n";
  
            byte[] h_c = new ASCIIEncoding().GetBytes(head);
            byte[] f_c = new ASCIIEncoding().GetBytes(foot);
  
            WebRequest request = (WebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = contentType;
  
  
            request.ContentLength = postBytes.Length+ h_c.Length+f_c.Length;
            using (Stream outstream = request.GetRequestStream())
            {
                outstream.Write(h_c, 0, h_c.Length);//输出head
                outstream.Write(postBytes, 0, postBytes.Length);//输出图片字节
                outstream.Write(f_c, 0, f_c.Length);//输出尾部
            }
            string result = string.Empty;
            using (WebResponse response = request.GetResponse())
            {
                if (response != null)
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
  
                }
            }
            return result;
        }

说明:

1)上面的代码中我的boundary=xxx是写死的,因为我对接的接口对方已经写死只获取这个分隔符.正常时候这里是会根据当前时间获取一个动态的字符串当做分隔符

2)输出的时候, outstream.Write 以前我的思路是先把参数拼接好以后直接一个输出就好了.结果拼接了好几个,发送出去以后对方都不能正常解析.最后在参考了一篇其他文章以后才恍然大悟.我可以分层次的输出呀.

备注:

下面放了一个很有启发的知识:

======================================开始

流:二进制

字节:无符号整数

字符:Unicode编码字符

字符串:多个Unicode编码字符

那么在.net下它们之间如何转化呢?

一般是遵守以下规则:

流->字节数组->字符数组->字符串

下面就来具体谈谈转化的语法

流->字节数组

MemoryStream ms = new MemoryStream();

byte[] buffer = new byte[ms.Length];

ms.Read(buffer, 0, (int)ms.Length);

字节数组->流

byte[] buffer = new byte[10];

MemoryStream ms = new MemoryStream(buffer);

字节数组->字符数组

1.

byte[] buffer = new byte[10];

char[] ch = new ASCIIEncoding().GetChars(buffer);

//或者:char[] ch = Encoding.UTF8.GetChars(buffer)

2.

byte[] buffer = new byte[10];

char[] ch = new char[10];

for(int i=0; i<buffer.Length; i++)

{

ch[i] = Convert.ToChar(buffer[i]);

}

字符数组->字节数组

1.

char[] ch = new char[10];

byte[] buffer = new ASCIIEncoding().GetBytes(ch);

//或者:byte[] buffer = Encoding.UTF8.GetBytes(ch)

2.

char[] ch = new char[10];

byte[] buffer = new byte[10];

for(int i=0; i<ch.Length; i++)

{

buffer[i] = Convert.ToByte(ch[i]);

}

字符数组->字符串

char[] ch = new char[10];

string str = new string(ch);

字符串->字符数组

string str = "abcde";

char[] ch=str .ToCharArray();

字节数组->字符串

byte[] buffer = new byte[10];

string str = System.Text.Encoding.UTF8.GetString(buffer);

//或者:string str = new ASCIIEncoding().GetString(buffer);

字符串->字节数组

string str = "abcde";

byte[] buffer=System.Text.Encoding.UTF8.GetBytes(str);

//或者:byte[] buffer= new ASCIIEncoding().GetBytes(str);

关于“C# form-data上传图片流到远程服务器怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI