• Unity C# 网络学习(七)——HTTP(二)


    Unity C# 网络学习(七)——HTTP(二)

    一.Post请求

            HttpWebRequest httpWebRequest = WebRequest.CreateHttp("http://192.168.1.103:8080/Http_Server/");
            httpWebRequest.Method = WebRequestMethods.Http.Post;
            httpWebRequest.Timeout = 2000;
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            string str = "Name=zzs&ID=2";
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            httpWebRequest.ContentLength = buffer.Length;
            Stream s = httpWebRequest.GetRequestStream();
            s.Write(buffer,0,buffer.Length);
            s.Close();
            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            if (httpWebResponse == null)
                return;
            Debug.Log(httpWebResponse.StatusCode);
            httpWebResponse.Close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    二.HTTP上传数据

        private void Start()
        {
            //上传数据不需要加上上传数据的名称
            HttpWebRequest httpWebRequest = WebRequest.CreateHttp(
                new Uri("http://192.168.1.103:8080/Http_Server/"));
            httpWebRequest.Method = WebRequestMethods.Http.Post;
            httpWebRequest.Timeout = 5000 * 100;
            //如果上传需要身份验证,需要同时设置httpWebRequest.PreAuthenticate = true;
            httpWebRequest.Credentials = new NetworkCredential("zzs", "123");
            httpWebRequest.PreAuthenticate = true;
            //设置内容的类型(复合类型,设置边界条件)
            httpWebRequest.ContentType = "multipart/form-data;boundary=zzs";
    
            //文件数据前的头部信息
            //--边界字符串
            //Content-Disposition:form-data;name="字段名字,之后写入文件的二进制数据和该字段名相对应";filename="传到服务器上使用的文件名"
            //Content-Type:application/octet-stream由于我们传二进制文件,所以这里使用二进制
            string head = "--zzs\r\n" +
                          "Content-Disposition:form-data;name=\"file\";filename=\"http上传的文件.jpg\"\r\n" +
                          "Content-Type:application/octet-stream\r\n\r\n";
            byte[] headBytes = Encoding.UTF8.GetBytes(head);
            //结束的边界信息
            //--边界字符串--
            byte[] endBytes = Encoding.UTF8.GetBytes("\r\n--zzs--\r\n");
    
            using (FileStream fs = new FileStream(Application.streamingAssetsPath + "/test.jpg", FileMode.Open))
            {
                byte[] buffer = new byte[10240];
                int len = fs.Read(buffer, 0, buffer.Length);
                Stream s = httpWebRequest.GetRequestStream();
                s.Write(headBytes, 0, headBytes.Length);
                while (len > 0)
                {
                    s.Write(buffer, 0, len);
                    len = fs.Read(buffer, 0, buffer.Length);
                }
    
                s.Write(endBytes, 0, endBytes.Length);
                s.Close();
            }
    
            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            if (httpWebResponse == null)
                return;
            Debug.Log(httpWebResponse.StatusCode == HttpStatusCode.OK ? "上传成功!" : "上传失败!");
        }
    
    • 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
    • 42
    • 43
    • 44
    • 45
    • 46

    三.HTTP上传数据封装

        public async void UpLoadFile(string fileName, string path, Action<HttpStatusCode> action)
        {
            HttpStatusCode code = HttpStatusCode.OK;
            await Task.Run(() =>
            {
                try
                {
                    HttpWebRequest httpWebRequest = WebRequest.CreateHttp(new Uri(RootPath));
                    httpWebRequest.Method = WebRequestMethods.Http.Post;
                    httpWebRequest.Timeout = 5000 * 100;
                    httpWebRequest.Credentials = new NetworkCredential(UserName, Password);
                    httpWebRequest.PreAuthenticate = true;
                    httpWebRequest.ContentType = "multipart/form-data;boundary=zzs";
    
                    string head = "--zzs\r\n" +
                                  "Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\n" +
                                  "Content-Type:application/octet-stream\r\n\r\n";
                    head = string.Format(head, fileName);
                    byte[] headBytes = Encoding.UTF8.GetBytes(head);
                    //结束的边界信息
                    //--边界字符串--
                    byte[] endBytes = Encoding.UTF8.GetBytes("\r\n--zzs--\r\n");
    
                    using (FileStream fs = new FileStream(path, FileMode.Open))
                    {
                        Stream s = httpWebRequest.GetRequestStream();
                        byte[] buffer = new byte[10240];
                        int len = fs.Read(buffer, 0, buffer.Length);
                        s.Write(headBytes,0,headBytes.Length);
                        while (len>0)
                        {
                            s.Write(buffer,0,len);
                            len = fs.Read(buffer, 0, buffer.Length);
                        }
                        s.Write(endBytes,0,endBytes.Length);
                        s.Close();
                    }
    
                    HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                    if (httpWebResponse == null)
                        return;
                    code = httpWebResponse.StatusCode;
                }
                catch (WebException we)
                {
                    Debug.Log(we);
                    code = HttpStatusCode.InternalServerError;
                }
            });
            action?.Invoke(code);
        }
    
    • 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
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    SpringBoot中使用Redis实现分布式锁
    Linux·驱动中的中断
    如何在kubernetes中实现分布式可扩展的WebSocket服务架构
    尚医通【预约挂号系统】总结
    linux内核的文件组织形式
    Flink处理函数 完整使用 (第七章)
    Day31-计算机基础1
    个人实现的可任意折叠QToolBox——AdvancedToolBox
    jupyter notebook中markdown改变图像大小
    14:00面试,14:06就出来了,问的问题有点变态。。。
  • 原文地址:https://blog.csdn.net/zzzsss123333/article/details/125456792