欢迎光临散文网 会员登陆 & 注册

c# form-data上传图片流到远程服务器

2022-08-30 09:34 作者:限量版范儿  | 我要投稿

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

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);

以上转的是 https://www.cnblogs.com/cc1120/p/9139454.html

======================================结束

在我的想法中,把所有的参数先转换为字符数组,然后拼接head,图片字节转换的字符数组,结束字符数组,然后把字符数组转换为字节进行发送.猜测应该是可行的,不过我自己在实际应用中是没有成功,可能是某一步转换格式的问题.因为项目要求太紧就没有继续尝试.

对了还有下面的一个知识点,有人知道是这样的吗?我是没有研究过不知道是不是.希望有大佬可以回答一下.

链接:https://www.dianjilingqu.com/508106.html

c# form-data上传图片流到远程服务器的评论 (共 条)

分享到微博请遵守国家法律