I wish to have a program that listen for posts on a specific port, e.g. http://xxx.xxx.xxx.xxx:60002?key=value
Where xxx.xxx.xxx.xxx
is running my program and the port it is listening to is 60002
. The program will then need to get to the params that were passed to it, in this case key
and value
I then want to be able to parse the values that come though. VB is not the language I normally use.
I want the solution to be compatible with .NET 3.5's framework.
Create a new class library project and you might want to use HttpPostRequestVB as the project and solution names. Rename the source file to HttpPostRequest to reflect the application that we want to develop. Add the following Imports directives. Add the HttpPostRequest class code that include the Main () subroutine.
The req.send () call appends the url-encoded string to the body of the HTTP message and sends the message to the specified HOST. When the HOST responds, the message is displayed in the form's textbox (Figure 9.3). Run the application by pressing F5, click the HTTP POST Request button, and you will see the following response in the textbox:
When communicating with a web service via HTTP POST, data is embedded as name-value pairs in the HTTP request's message body. Unlike HTTP GET, the name-value pairs do not appear as part of the URI.
Once the HttpWebRequest object is created, ' request stream closed. The response is then retrieved. Console.WriteLine ("Creating HTTP web request...") httpRequest = CType(WebRequest.Create (postUrl), HttpWebRequest) Console.WriteLine ("Changing method from the default GET to POST...")
Slight modification of the following code snippet (from http://social.msdn.microsoft.com/Forums/vstudio/en-US/b7f476d1-3147-4b18-ba5e-0b3ce8f8a918/want-to-make-a-webserver-with-httplistener ) worked for me:
Imports System.Net
Imports System.Globalization
Module HttpListener
Sub Main()
Dim prefixes(0) As String
prefixes(0) = "http://*:8080/HttpListener/"
ProcessRequests(prefixes)
End Sub
Private Sub ProcessRequests(ByVal prefixes() As String)
If Not System.Net.HttpListener.IsSupported Then
Console.WriteLine( _
"Windows XP SP2, Server 2003, or higher is required to " & _
"use the HttpListener class.")
Exit Sub
End If
' URI prefixes are required,
If prefixes Is Nothing OrElse prefixes.Length = 0 Then
Throw New ArgumentException("prefixes")
End If
' Create a listener and add the prefixes.
Dim listener As System.Net.HttpListener = _
New System.Net.HttpListener()
For Each s As String In prefixes
listener.Prefixes.Add(s)
Next
Try
' Start the listener to begin listening for requests.
listener.Start()
Console.WriteLine("Listening...")
' Set the number of requests this application will handle.
Dim numRequestsToBeHandled As Integer = 10
For i As Integer = 0 To numRequestsToBeHandled
Dim response As HttpListenerResponse = Nothing
Try
' Note: GetContext blocks while waiting for a request.
Dim context As HttpListenerContext = listener.GetContext()
' Create the response.
response = context.Response
Dim responseString As String = _
"<HTML><BODY>The time is currently " & _
DateTime.Now.ToString( _
DateTimeFormatInfo.CurrentInfo) & _
"</BODY></HTML>"
Dim buffer() As Byte = _
System.Text.Encoding.UTF8.GetBytes(responseString)
response.ContentLength64 = buffer.Length
Dim output As System.IO.Stream = response.OutputStream
output.Write(buffer, 0, buffer.Length)
Catch ex As HttpListenerException
Console.WriteLine(ex.Message)
Finally
If response IsNot Nothing Then
response.Close()
End If
End Try
Next
Catch ex As HttpListenerException
Console.WriteLine(ex.Message)
Finally
' Stop listening for requests.
listener.Close()
Console.WriteLine("Done Listening...")
End Try
End Sub
End Module
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With