Ivan Climov

Ivan Climov

  • 1.1k
  • 692
  • 22.4k

How to make the data (JSON) from the POST request automatica

Jul 4 2019 1:55 PM
I try to make a WinForm application with a web server.
Did so:
1. I use github.com - "eske / SimpleHttpServer" - link
(https://github.com/jeske/SimpleHttpServer).
2. Created a WinForm project.
3. Added a link to the library project SimpleHttpServer
.. \ SimpleHttpServer \ bin \ Debug \ SimpleHttpServer.dll
4. Added code to Form1. (see below "My Code").
Questions
 
1. Have I correctly connected the server and WinForm?
2. How to make the data (JSON) that is transmitted in a POST request to my server automatically fall into the variable I need in WinForm?
 
In other words: as soon as the POST request arrives at the server, the data is automatically placed in the variable.
My goal: to get acquainted with the principles of work Viber Api.
For this, I propose to host the application on the local computer.
WinWorm (user interface), which will interact with the server Viber Api). Everything is placed on one computer and used by one user.
With the help of Ngrok application will be open to the Internet.
The Ngrok service provides links forhttp: // localhost: XXXXX links like https: // 6eb5a091.ngrok.io /
My code.
  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.     StartHttpServer();  
  4. }
  5. static void StartHttpServer()  
  6. {  
  7.             try  
  8.             {             
  9.    
  10.                 // var route_config = new List()  
  11.                 var route_config = new List()  
  12.                 {  
  13.                     new Route  
  14.                     {  
  15.                         Name = "?????? ?????????? (Hello Handler)",  
  16.                         UrlRegex = @"^/$",  
  17.                         Method = "GET",  
  18.    
  19.                         Callable = (HttpRequest request) =>  
  20.                         {  
  21.                             return new HttpResponse()  
  22.                             {  
  23.                                 ContentAsUTF8 = "?????? ?? ???????? ??????? Http (Hello from SimpleHttpServer)",  
  24.                                 ReasonPhrase = "OK",  
  25.                                 StatusCode = "200"  
  26.                             };  
  27.                         }  
  28.                     },   
  29.                     //new Route {     
  30.                     //    Name = "FileSystem Static Handler",  
  31.                     //    UrlRegex = @"^/Static/(.*)$",  
  32.                     //    Method = "GET",  
  33.                     //    Callable = new FileSystemRouteHandler() { BasePath = @"C:\Tmp", ShowDirectories=true }.Handle,  
  34.                     //},  
  35.                 };  
  36.    
  37.                     HttpServer httpServer = new HttpServer(8080, route_config);  
  38.    
  39.                     Thread thread = new Thread(new ThreadStart(httpServer.Listen));  
  40.                     thread.Start();  
  41.             }  
  42.             catch (Exception ex)  
  43.             {  
  44.                 string s = ex.Message;  
  45.                 string t = ex.StackTrace;  
  46.                 // throw;  
  47.                 MessageBox.Show(s + " \r\n "  
  48.                                   + t);  
  49.             }
  50.         }

Answers (3)