• unity简单http案例 + 服务端


    客户端

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Net;
    using System.Text;
    using System.Threading;
    using Newtonsoft.Json;
    using UnityEngine;
    
    public class TestHttp : MonoBehaviour
    {
        private WebClient wc;
        private string url = "http://localhost:8079";
        private Thread thread;
        
        // Start is called before the first frame update
        void Start()
        {
            CreateWebClient();
            thread = new Thread(SendHttpMsg);
            Debug.Log("按K键发送消息");
        }
    
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.K))
            {
                //异步
                Action ac = SendHttpMsg;
                ac.BeginInvoke(null,null);
                
                //同步
                //SendHttpMsg();
    
                //线程
                //thread.Start();
            }
        }
    
        void CreateWebClient()
        {
            wc = new WebClient();
        }
        
        /// <summary>
        /// 向服务器发送消息
        /// </summary>
        void SendHttpMsg()
        {
            Debug.Log($"请求服务地址:{url},时间:{DateTime.Now.ToString()}");
            //模拟一个json数据发送到服务端
            var data = new Data(1, "张三");
            var jsonModel = JsonConvert.SerializeObject(data);
            //发送到服务端并获得返回值
            byte[] returnInfo;
            try
            {
                returnInfo = wc.UploadData(url, Encoding.UTF8.GetBytes(jsonModel));
            }
            catch (Exception e)
            {
                Debug.LogError("url可能不对,或者远程服务器关闭,或者连接失败");
                Debug.LogError(e);
                return;
            }
            //把服务端返回的信息转成字符串
            var str = Encoding.UTF8.GetString(returnInfo);
            Debug.Log($"服务端返回信息:{str},时间:{DateTime.Now.ToString()}");
        }
    }
    
    class Data
    {
        public Data(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
        public int ID { get; set; }
    
        public string Name { get; set; }
    }

    服务端

    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Text;
    
    namespace HttpServer
    {
        internal class Program
        {
            static HttpListener httpobj;
            public static void Main(string[] args)
            {
                //提供一个简单的、可通过编程方式控制的 HTTP 协议侦听器。此类不能被继承。
                httpobj = new HttpListener();
                //定义url及端口号,通常设置为配置文件
                //httpobj.Prefixes.Add("http://+:8089/");
                httpobj.Prefixes.Add("http://localhost:8079/");
    
    
                //启动监听器
                httpobj.Start();
                //异步监听客户端请求,当客户端的网络请求到来时会自动执行Result委托
                //该委托没有返回值,有一个IAsyncResult接口的参数,可通过该参数获取context对象
                httpobj.BeginGetContext(Result, null);
                Console.WriteLine($"服务端初始化完毕,正在等待客户端请求,时间:{DateTime.Now.ToString()}
    ");
                Console.ReadKey();
            }
            
            private static void Result(IAsyncResult ar)
            {
                //当接收到请求后程序流会走到这里
    
                //继续异步监听
                httpobj.BeginGetContext(Result, null);
                var guid = Guid.NewGuid().ToString();
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine($"接到新的请求:{guid},时间:{DateTime.Now.ToString()}");
                //获得context对象
                var context = httpobj.EndGetContext(ar);
                var request = context.Request;
                var response = context.Response;
                ////如果是js的ajax请求,还可以设置跨域的ip地址与参数
                //context.Response.AppendHeader("Access-Control-Allow-Origin", "*");//后台跨域请求,通常设置为配置文件
                //context.Response.AppendHeader("Access-Control-Allow-Headers", "ID,PW");//后台跨域参数设置,通常设置为配置文件
                //context.Response.AppendHeader("Access-Control-Allow-Method", "post");//后台跨域请求设置,通常设置为配置文件
                context.Response.ContentType = "text/plain;charset=UTF-8";//告诉客户端返回的ContentType类型为纯文本格式,编码为UTF-8
                context.Response.AddHeader("Content-type", "text/plain");//添加响应头信息
                context.Response.ContentEncoding = Encoding.UTF8;
                string returnObj = null;//定义返回客户端的信息
                if (request.HttpMethod == "POST" && request.InputStream != null)
                {
                    //处理客户端发送的请求并返回处理信息
                    returnObj = HandleRequest(request, response);
                }
                else
                {
                    returnObj = $"不是post请求或者传过来的数据为空";
                }
                var returnByteArr = Encoding.UTF8.GetBytes(returnObj);//设置客户端返回信息的编码
                try
                {
                    using (var stream = response.OutputStream)
                    {
                        //把处理信息返回到客户端
                        stream.Write(returnByteArr, 0, returnByteArr.Length);
                    }
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"网络蹦了:{ex.ToString()}");
                }
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine($"请求处理完成:{guid},时间:{ DateTime.Now.ToString()}
    ");
            }
    
            private static string HandleRequest(HttpListenerRequest request, HttpListenerResponse response)
            {
                string data = null;
                try
                {
                    var byteList = new List<byte>();
                    var byteArr = new byte[2048];
                    int readLen = 0;
                    int len = 0;
                    //接收客户端传过来的数据并转成字符串类型
                    do
                    {
                        readLen = request.InputStream.Read(byteArr, 0, byteArr.Length);
                        len += readLen;
                        byteList.AddRange(byteArr);
                    } while (readLen != 0);
                    data = Encoding.UTF8.GetString(byteList.ToArray(),0, len);
    
                    //获取得到数据data可以进行其他操作
                }
                catch (Exception ex)
                {
                    response.StatusDescription = "404";
                    response.StatusCode = 404;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"在接收数据时发生错误:{ex.ToString()}");
                    return $"在接收数据时发生错误:{ex.ToString()}";//把服务端错误信息直接返回可能会导致信息不安全,此处仅供参考
                }
                response.StatusDescription = "200";//获取或设置返回给客户端的 HTTP 状态代码的文本说明。
                response.StatusCode = 200;// 获取或设置返回给客户端的 HTTP 状态代码。
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine($"接收数据完成:{data.Trim()},时间:{DateTime.Now.ToString()}");
                return $"接收数据完成";
            }
        }
    }
  • 相关阅读:
    15 react ajax 请求 github 用户信息
    14 react fetch
    13 React axios
    12 脚手架编写React项目(评论管理)---
    gitlab init project
    为什么是2MSL而不是MSL?
    mac python install zlib not available
    Laravel 传递数据到视图
    sleep(0)作用
    ping错误详解
  • 原文地址:https://www.cnblogs.com/sanyejun/p/14829776.html
Copyright © 2020-2023  润新知