///<summary> ///向服務器發送混合型的請求,1:成功發送,0:發送失敗 ///</summary> ///<param name="paranames">表單參數名數組</param> ///<param name="paravalues">參數值數組</param> ///<param name="files">文件名數組</param> ///<param name="errmsg">報錯信息</param> ///<returns></returns> public int SendRequest(string[] paranames, string[] paravalues, string[] files, ref string errmsg) { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Connect(new IPEndPoint(IPAddress.Parse("192.168.0.201"), 8085)); StringBuilder http, text; byte[] httpbyte; byte[] textbyte = null; long length = 0; DateTime now = DateTime.Now; List<byte[]> data = new List<byte[]>(); //構造時間戳 string strBoundary = "------------" + DateTime.Now.Ticks.ToString("x"); byte [] end = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n");//結束標記 byte[] boundary = Encoding.ASCII.GetBytes("\r\n" + strBoundary + "\r\n"); //加載表單參數信息 if (paranames != null) { text = new StringBuilder(); for (int i = 0; i < paranames.Length; i++) { text.Append("--"); text.Append(strBoundary);//添加時間戳 text.Append("\r\n"); text.Append("Content-Disposition: form-data; name=\"" + paranames[i] + "\"\r\n\r\n"); text.Append(paravalues[i]); text.Append("\r\n"); } string para = text.ToString(); textbyte = Encoding.ASCII.GetBytes(para); length += textbyte.Length; } //加載文件信息 if (files != null) { for (int i = 0; i < files.Length; i++) { FileStream fs; StringBuilder sbfile = new StringBuilder(); try { fs = File.Open(files[i], FileMode.Open, FileAccess.Read, FileShare.Read); if (i == 0) sbfile.Append("--");//添加文件 else sbfile.Append("\r\n--"); sbfile.Append(strBoundary);//添加時間戳 sbfile.Append("\r\n"); sbfile.Append("Content-Disposition: form-data; name=\""); sbfile.Append("file"); sbfile.Append("\"; filename=\""); sbfile.Append("2222.png"); sbfile.Append("\""); sbfile.Append("\r\n"); sbfile.Append("Content-Type: "); sbfile.Append("application/octet-stream"); sbfile.Append("\r\nContent-Length:"); sbfile.Append(fs.Length.ToString()); sbfile.Append("\r\n"); sbfile.Append("\r\n"); string temp = sbfile.ToString(); byte[] bin = Encoding.UTF8.GetBytes(temp); data.Add(bin); length += bin.Length; length += fs.Length; fs.Close(); } catch (Exception exc) { errmsg = exc.Message.ToString(); return 0; } } } //構造http頭 http = new StringBuilder(); http.Append("POST http://192.168.0.201:8085/api/file/upload HTTP/1.1\r\n"); http.Append("Content-Type:multipart/form-data;boundary="); http.Append(strBoundary); http.Append("\r\n"); http.Append("Host: 192.168.0.201:8085\r\n"); http.Append("Content-Length:"); http.Append((length+end.Length).ToString()); http.Append("\r\n"); http.Append("Expect: 100-continue\r\n");//註明要在收到服務器的continue消息後才繼續上傳http消息體 http.Append("Connection: Keep-Alive\r\n\r\n"); string strtemp = http.ToString(); httpbyte = Encoding.ASCII.GetBytes(strtemp); try { s.Send(httpbyte);// "//首先發送http頭 Thread.Sleep(100); //string check = GetResponse(); //if (check == null || !check.Contains("Continue"))//獲得服務器確認後才繼續上傳 //{ // errmsg = "客戶端已成功發送請求,但服務器沒有響應!"; // return 0; //} if (paranames != null) { s.Send(textbyte, textbyte.Length, SocketFlags.None);//發送表單參數 } if (files != null) {//依次發送文件 for (int i = 0; i < data.Count; i++) { int size = 0; FileStream fs = File.Open(files[i], FileMode.Open, FileAccess.Read, FileShare.Read); s.Send(data[i], data[i].Length, SocketFlags.None); byte[] buff = new byte[1024]; size = fs.Read(buff, 0, 1024); while (size > 0) { s.Send(buff, size, SocketFlags.None); size = fs.Read(buff, 0, 1024); } fs.Close(); } } s.Send(end, end.Length, SocketFlags.None); return 1; } catch (Exception exc) { errmsg = exc.Message.ToString(); return 0; } }
C# Socket Post 提交文件到服務器上 其中最重要的就是api
上傳文件的時候必定要主要提交結束標記數組
就是上文中的end服務器