Reputation: 1
I have to write in my VB.Net app a code who wait for an http request, and, when request comes, extract JSON string from it.
I know how to process JSON String
I know how to code parallel thread to wait for request.
But I don't know how to catch the request
I found this code : How to wait for multiple async http request
but it's an old code (Microsoft now says that classes HttpWebResponse and HttpWebRequest used in this example are deprecated), and it seems to be hard to translate in VB : there's an instruction like "foreach(var result in completedTasks.Select(t=>t.Result))" and "t" isn't defined anywhere.
I found others examples but they all tell we how to send Http Request from Vb.Net, and I wanna receive the request in VB.Net app, and not send it from VB.Net.
Can anyone help me please ?
Thanks
Upvotes: 0
Views: 832
Reputation: 74605
Most recently I did this with EmbedIO from Unosquare. Install the EmbedIO package using Nuget, into a new win forms (doesn't have to be; you can rip this apart later, but I used winforms to create this demo), then paste all this code over the top of your Form1.vb:
Imports EmbedIO
Imports EmbedIO.Routing
Imports EmbedIO.WebApi
Public Class Form1
Private server As WebServer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim url = "http://localhost:9696/"
server = CreateWebServer(url)
server.RunAsync()
End Sub
' Create and configure our web server.
Private Shared Function CreateWebServer(ByVal url As String) As WebServer
' First, we will configure our web server by adding Modules.
Dim server = New WebServer(
Function(o)
Return o.WithUrlPrefix(url).WithMode(HttpListenerMode.Microsoft)
End Function
) _
.WithWebApi("/api",
Function(m)
Return m.WithController(Of ApiController)()
End Function
)
Return server
End Function
End Class
Public Class ApiController
Inherits WebApiController
<Route(HttpVerbs.Post, "/data")>
Public Async Function PostJsonData() As Task(Of ActionResult)
Dim data = Await HttpContext.GetRequestDataAsync(Of MyJsonThing)()
'install some tool like PostMan and POST some json data, like { "MyJsonThing": { "SomeProperty": "Hello" } }
'do stuff here
'return a response here
End Function
<Route(HttpVerbs.Get, "/ping")>
Public Function Ping() As String
Return "pong"
End Function
End Class
Public Class MyJsonThing
Public SomeProperty As String
End Class
Then run the app, open a web browser and navigate to http://localhost:9696/api/ping
You should see "pong"; if not check your firewall
The rest, is up to you!
Upvotes: 1