溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#?form-data上傳圖片流到遠(yuǎn)程服務(wù)器怎么實現(xiàn)

發(fā)布時間:2022-08-30 15:14:42 來源:億速云 閱讀:152 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“C# form-data上傳圖片流到遠(yuǎn)程服務(wù)器怎么實現(xiàn)”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強(qiáng),希望這篇“C# form-data上傳圖片流到遠(yuǎn)程服務(wù)器怎么實現(xiàn)”文章能幫助大家解決問題。

先貼代碼,后面做一些簡單說明:

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

說明:

1)上面的代碼中我的boundary=xxx是寫死的,因為我對接的接口對方已經(jīng)寫死只獲取這個分隔符.正常時候這里是會根據(jù)當(dāng)前時間獲取一個動態(tài)的字符串當(dāng)做分隔符

2)輸出的時候, outstream.Write 以前我的思路是先把參數(shù)拼接好以后直接一個輸出就好了.結(jié)果拼接了好幾個,發(fā)送出去以后對方都不能正常解析.最后在參考了一篇其他文章以后才恍然大悟.我可以分層次的輸出呀.

備注:

下面放了一個很有啟發(fā)的知識:

======================================開始

流:二進(jìn)制

字節(jié):無符號整數(shù)

字符:Unicode編碼字符

字符串:多個Unicode編碼字符

那么在.net下它們之間如何轉(zhuǎn)化呢?

一般是遵守以下規(guī)則:

流->字節(jié)數(shù)組->字符數(shù)組->字符串

下面就來具體談?wù)勣D(zhuǎn)化的語法

流->字節(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);

關(guān)于“C# form-data上傳圖片流到遠(yuǎn)程服務(wù)器怎么實現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI