zoukankan      html  css  js  c++  java
  • C# Socket Post File

         ///<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("
    --" + strBoundary + "--
    ");//结束标记
                byte[] boundary = Encoding.ASCII.GetBytes("
    " + strBoundary + "
    ");
    
    
                //加载表单参数信息
    
                if (paranames != null)
    
                {
    
                    text = new StringBuilder();
    
                    for (int i = 0; i < paranames.Length; i++)
    
                    {
    
                        text.Append("--");
    
                        text.Append(strBoundary);//添加时间戳
    
                        text.Append("
    ");
    
                        text.Append("Content-Disposition: form-data; name="" + paranames[i] + ""
    
    ");
    
                        text.Append(paravalues[i]);
    
                        text.Append("
    ");
    
                    }
    
                    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("
    --");
    
                            sbfile.Append(strBoundary);//添加时间戳                       
    
                            sbfile.Append("
    ");
    
                            sbfile.Append("Content-Disposition: form-data; name="");
    
                            sbfile.Append("file");
    
                            sbfile.Append(""; filename="");
    
                            sbfile.Append("2222.png");
    
                            sbfile.Append(""");
    
                            sbfile.Append("
    ");
    
                            sbfile.Append("Content-Type: ");
    
                            sbfile.Append("application/octet-stream");
    
                            sbfile.Append("
    Content-Length:");
    
                            sbfile.Append(fs.Length.ToString());
    
                            sbfile.Append("
    ");
    
                            sbfile.Append("
    ");
    
                            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
    ");
    
                http.Append("Content-Type:multipart/form-data;boundary=");
    
                http.Append(strBoundary);
    
                http.Append("
    ");
    
                http.Append("Host: 192.168.0.201:8085
    ");
    
                http.Append("Content-Length:");
    
                http.Append((length+end.Length).ToString());
    
                http.Append("
    ");
    
                http.Append("Expect: 100-continue
    ");//注明要在收到服务器的continue消息后才继续上传http消息体
    
                http.Append("Connection: Keep-Alive
    
    ");
    
                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 提交文件到服务器上 其中最重要的就是

    上传文件的时候一定要主要提交结束标记

    就是上文中的end

  • 相关阅读:
    Oracle在Linux下使用异步IO(aio)配置
    慢慢聊Linux AIO
    ssh批量登录并执行命令(python实现)
    Reverse Linked List II
    Reverse Linked List
    Excel Sheet Column Title
    Summary Ranges
    面试题1:实现大数加减乘除四则运算
    Multiply Strings
    Evaluate Reverse Polish Notation
  • 原文地址:https://www.cnblogs.com/keepsilence/p/6813350.html
Copyright © 2011-2022 走看看