c# form-data上传图片流到远程服务器
先贴代码,后面做一些简单说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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,图片字节转换的字符数组,结束字符数组,然后把字符数组转换为字节进行发送.猜测应该是可行的,不过我自己在实际应用中是没有成功,可能是某一步转换格式的问题.因为项目要求太紧就没有继续尝试.
对了还有下面的一个知识点,有人知道是这样的吗?我是没有研究过不知道是不是.希望有大佬可以回答一下.
以上内容来源于百科书,可以关注我了解更多.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库