简单的Http服务器监听和客户端发送

服务器端:

   static void Main(string[] args)
        {
            StartTcpListener();
        }

        static void StartTcpListener()
        {
           string url =  ConfigurationSettings.AppSettings.Get("ListenUrl");
            if (string.IsNullOrEmpty(url))
            {
                return;
            }

            HttpListener listener = new HttpListener();
            listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
            listener.Prefixes.Add(url);
            listener.Start(); 
            new Thread(new ThreadStart(delegate
            {
                while (true)
                {
                    HttpListenerContext httpListenerContext = listener.GetContext();
                    httpListenerContext.Response.StatusCode = 200;
                    using (StreamReader sr = new StreamReader(httpListenerContext.Request.InputStream))
                    {
                        string str = sr.ReadToEnd();
                        Console.WriteLine(str);
                    }
                    using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                    {
                        writer.Write("success");
                    }
                    Thread.Sleep(100);
                }
            })).Start();
        }

客户端:

        static void Main(string[] args)
        {

            string url = ConfigurationSettings.AppSettings.Get("ListenUrl");
            if (string.IsNullOrEmpty(url))
            {
                return;
            }

            while(true)
            {
                string req = "{\"IDCode\":\"38409387401840\",\"TicketCode\":\"398429438792\",\"TName\":\"张三\"}";
                string resp = Request(url, req);
                Console.WriteLine(resp);
                Thread.Sleep(1000);
            }
        }



        private static string Request(string url, string content, string method = "POST") 
        {
            try
            {
                HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
                req.ContentType = "application/json; charset=utf-8";
                req.Method = method;
                req.Timeout = 50000;
                if (content != null)
                {
                    using (Stream rs = req.GetRequestStream())
                    {
                        byte[] bytes = Encoding.UTF8.GetBytes(content);
                        rs.Write(bytes, 0, bytes.Length);
                    }
                }
               
                using (Stream rs = req.GetResponse().GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(rs))
                    {
                        string strResult = sr.ReadToEnd();
                        return strResult;
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
#if debug
                throw ex;
#endif
                return null;
            }
        }
测试结果:

简单的Http服务器监听和客户端发送_第1张图片

你可能感兴趣的:(C#)