I need to transfer the data from HTTPServer toWinForm.
I use HTTPServer (https://github.com/jeske/SimpleHttpServer)
I transferred the project SimpleHttpServer (https://github.com/jeske/SimpleHttpServer/tree/master/SimpleHttpServer) in your project WinForm.
I run HTTPServer with the following code.
Code.
- private void Form1_Load(object sender, EventArgs e)
- {
- StartHttpServer();
- }
-
- static void StartHttpServer()
- {
- try
- {
- var route_config = new List();
- HttpServer httpServer = new HttpServer(8080, route_config);
-
- Thread thread = new Thread(new ThreadStart(httpServer.Listen));
- thread.Start();
- }
- catch (Exception ex)
- {
- string s = ex.Message;
- string t = ex.StackTrace;
-
- MessageBox.Show(s + " \r\n " +
- t);
- }
- }
If the server receives a POST request with JSON, then the request comes in the class HttpProcessor.cs method HandleClient.
Code
- public void HandleClient(TcpClient tcpClient)
- {
- Stream inputStream = GetInputStream(tcpClient);
- Stream outputStream = GetOutputStream(tcpClient);
- HttpRequest request = GetRequest(inputStream, outputStream);
-
- #region ??? ?????????.
-
- RequestContent?.Invoke(request);
- #endregion
-
-
- HttpResponse response = RouteRequest(inputStream, outputStream, request);
-
-
-
-
- if (response.Content == null)
- {
- if (response.StatusCode != "200")
- {
- response.ContentAsUTF8 = string.Format("{0} {1}
{2}", response.StatusCode, request.Url, response.ReasonPhrase);
- }
- }
-
- WriteResponse(outputStream, response);
-
- outputStream.Flush();
- outputStream.Close();
- outputStream = null;
-
- inputStream.Close();
- inputStream = null;
-
- }
To get JSON from the HttpProcessor.cs class method HandleClient.
I plan to use the public event Action RequestContent;.
Code
- public event Action RequestContent;
-
- public void HandleClient(TcpClient tcpClient)
- {
-
- RequestContent?.Invoke(request);
-
- }
Question
1. How to transfer data from the third-level class (the HttpProcessor.cs class) to the first-level class (the WinForm class)?
2. How are there other solutions to replace using the public event Action RequestContent;?