C#?form-data上傳圖片流到遠(yuǎn)程服務(wù)器的詳細(xì)代碼
先貼代碼,后面做一些簡(jiǎn)單說(shuō)明:
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);//輸出圖片字節(jié) 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; }
說(shuō)明:
1)上面的代碼中我的boundary=xxx是寫死的,因?yàn)槲覍?duì)接的接口對(duì)方已經(jīng)寫死只獲取這個(gè)分隔符.正常時(shí)候這里是會(huì)根據(jù)當(dāng)前時(shí)間獲取一個(gè)動(dòng)態(tài)的字符串當(dāng)做分隔符
2)輸出的時(shí)候, outstream.Write 以前我的思路是先把參數(shù)拼接好以后直接一個(gè)輸出就好了.結(jié)果拼接了好幾個(gè),發(fā)送出去以后對(duì)方都不能正常解析.最后在參考了一篇其他文章以后才恍然大悟.我可以分層次的輸出呀.
備注:
下面放了一個(gè)很有啟發(fā)的知識(shí):
======================================開始
流:二進(jìn)制
字節(jié):無(wú)符號(hào)整數(shù)
字符:Unicode編碼字符
字符串:多個(gè)Unicode編碼字符
那么在.net下它們之間如何轉(zhuǎn)化呢?
一般是遵守以下規(guī)則:
流->字節(jié)數(shù)組->字符數(shù)組->字符串
下面就來(lái)具體談?wù)勣D(zhuǎn)化的語(yǔ)法
流->字節(jié)數(shù)組
MemoryStream ms = new MemoryStream(); byte[] buffer = new byte[ms.Length]; ms.Read(buffer, 0, (int)ms.Length);
字節(jié)數(shù)組->流
byte[] buffer = new byte[10]; MemoryStream ms = new MemoryStream(buffer);
字節(jié)數(shù)組->字符數(shù)組
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]); }
字符數(shù)組->字節(jié)數(shù)組
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]); }
字符數(shù)組->字符串
char[] ch = new char[10]; string str = new string(ch);
字符串->字符數(shù)組
string str = "abcde"; char[] ch=str .ToCharArray();
字節(jié)數(shù)組->字符串
byte[] buffer = new byte[10]; string str = System.Text.Encoding.UTF8.GetString(buffer); //或者:string str = new ASCIIEncoding().GetString(buffer);
字符串->字節(jié)數(shù)組
string str = "abcde"; byte[] buffer=System.Text.Encoding.UTF8.GetBytes(str); //或者:byte[] buffer= new ASCIIEncoding().GetBytes(str);
以上轉(zhuǎn)的是 https://www.cnblogs.com/cc1120/p/9139454.html
======================================結(jié)束
在我的想法中,把所有的參數(shù)先轉(zhuǎn)換為字符數(shù)組,然后拼接head,圖片字節(jié)轉(zhuǎn)換的字符數(shù)組,結(jié)束字符數(shù)組,然后把字符數(shù)組轉(zhuǎn)換為字節(jié)進(jìn)行發(fā)送.猜測(cè)應(yīng)該是可行的,不過(guò)我自己在實(shí)際應(yīng)用中是沒有成功,可能是某一步轉(zhuǎn)換格式的問題.因?yàn)轫?xiàng)目要求太緊就沒有繼續(xù)嘗試.
對(duì)了還有下面的一個(gè)知識(shí)點(diǎn),有人知道是這樣的嗎?我是沒有研究過(guò)不知道是不是.希望有大佬可以回答一下.
到此這篇關(guān)于C#form-data上傳圖片流到遠(yuǎn)程服務(wù)器的文章就介紹到這了,更多相關(guān)C#form-data上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#使用FreeSql生產(chǎn)環(huán)境時(shí)自動(dòng)升級(jí)備份數(shù)據(jù)庫(kù)
使用FreeSql,包含所有的ORM數(shù)據(jù)庫(kù),都會(huì)存在這樣的問題。在codefirst模式下,根據(jù)代碼自動(dòng)更新數(shù)據(jù)庫(kù),都建議不要在生產(chǎn)環(huán)境使用。因?yàn)槿菀讈G失數(shù)據(jù),本文提供一種自動(dòng)更新數(shù)據(jù)庫(kù)的解決的思路:在判斷需要升級(jí)時(shí),才自動(dòng)升級(jí),同時(shí)升級(jí)前先備份數(shù)據(jù)庫(kù)2021-06-06C#使用委托(delegate)實(shí)現(xiàn)在兩個(gè)form之間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了C#使用委托(delegate)實(shí)現(xiàn)在兩個(gè)form之間傳遞數(shù)據(jù)的方法,涉及C#委托的使用技巧,需要的朋友可以參考下2015-04-04C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
這篇文章主要介紹了C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法,需要的朋友可以參考下2015-09-09C#中通過(guò)反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法
本文主要介紹了C#中通過(guò)反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09C#程序啟動(dòng)項(xiàng)的設(shè)置方法
這篇文章主要為大家詳細(xì)介紹了C#程序啟動(dòng)項(xiàng)的設(shè)置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探
這篇文章主要為大家詳細(xì)介紹了C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03