httpListener, AsyncCallback syntax

This forum is meant for examples of X# code.

User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

httpListener, AsyncCallback syntax

Post by wriedmann »

// C# code see here:
// http://mikehadlow.blogspot.it/2006/07/p ... tpsys.html
//
// to make it run without admin rights you need something like this one:
// netsh http add urlacl url="http://*:8080/" user=[username] listen=yes

#using System
#using System.Net
#using System.Text
#using System.IO
#using XsHttpListener

begin namespace XsHttpListener

function Start() as void
local oProgram as MainProgram

oProgram := MainProgram{}
oProgram:Start()

return

class MainProgram
protect _oListener as HttpListener

constructor()

_oListener := HttpListener{}

return

method Start() as void

_oListener:Prefixes:Add( "http://*:8080/" )
_oListener:Start()
Console.WriteLine( "Listening on port 8080, hit enter to stop" )
// Vulcan.NET needs this syntax
//_oListener:BeginGetContext( AsyncCallback{ self, @GetContextCallback() }, null )
_oListener:BeginGetContext( AsyncCallback{ self:GetContextCallback }, null )
Console.ReadLine()
_oListener:Stop()

return

method GetContextCallback( oResult as IAsyncResult ) as void
local oContext as HttpListenerContext
local oRequest as HttpListenerRequest
local oResponse as HttpListenerResponse
local oSB as StringBuilder
local oString as string
local oBuffer as byte[]
local oOutput as Stream

oContext := _oListener:EndGetContext( oResult )
oRequest := oContext:Request
oResponse := oContext:Response

oSB := StringBuilder{}
oSB:Append( e"n" )
oSB:AppendFormat( e"HttpMethod: {0}n", oRequest:HttpMethod )
oSB:AppendFormat( e"URI: {0}n", oRequest:Url:AbsoluteUri )
oSB:AppendFormat( e"Local path: {0}n", oRequest:Url:LocalPath )
oSB:Append( e"n" )
foreach cKey as string in oRequest:QueryString:Keys
oSB:AppendFormat( e"Query: {0} = {1}n", cKey, oRequest:QueryString[cKey] )
next
oSB:Append( e"n" )

oString := oSB:ToString()
oBuffer := System.Text.Encoding.UTF8:GetBytes( oString )
oResponse:ContentLength64 := oBuffer:Length
oOutput := oResponse:OutputStream
oOutput:Write( oBuffer, 0, oBuffer:Length )

_oListener:BeginGetContext( AsyncCallback{ self, @GetContextCallback() }, null )

return

end class

end namespace
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Horst
Posts: 337
Joined: Tue Oct 13, 2015 3:27 pm

httpListener, AsyncCallback syntax

Post by Horst »

Hi Wolfgang

I tried this Listerner now and it works great. I have some questions about the style you wrote the code.

You are using the class StringBuilder, why not -> cString := "HttpMethod: "+oRequest:HttpMethod+"n" ?
And you are using oString as string why not cString as string ?

Horst
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

httpListener, AsyncCallback syntax

Post by wriedmann »

Hi Horst,

using the StringBuilder class to concat strings is more efficient that "adding" them - at least I was told so.

Sometimes the use of the StringBuilder class makes the code more readable.

But do what you prefer...

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
kitz
Posts: 87
Joined: Wed Nov 29, 2017 8:56 am

httpListener, AsyncCallback syntax

Post by kitz »

Hello Wolfgang!
I also use a HTTPListener like that and it worked ok.
But recently I tried to change the targeted .NET version from 3.5 to 4.0 and now have problems:
No problems at all in the code or in my program, no try/catch fires, but the sender gets an error "HTTP Status 400 bad request" which doesn't happen with the old version. My own test sender works like before, so I can't test it myself.
Do you happen t know if there has been a change in .NET between versions 3.5 and 4.0?
BR Kurt
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

httpListener, AsyncCallback syntax

Post by wriedmann »

Hi Kurt,

unfortunately I don't know about that as I have never used .NET 3.5 - I have started to work with 4.0 and also my sample is for .NET 4.
Do you have used netsh like this here:
// to make it run without admin rights you need something like this one:
// netsh http add urlacl url="http://*:8080/" user=[username] listen=yes

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Horst
Posts: 337
Joined: Tue Oct 13, 2015 3:27 pm

httpListener, AsyncCallback syntax

Post by Horst »

Hallo Wolfgang
The Listerner runs, but when i leave the server its stops working. How i have to start this Listerner?

Horst
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

httpListener, AsyncCallback syntax

Post by wriedmann »

Hi Horst,
you have to build and install it as service. Or you can run it as task (Aufgabe).

You can find a sample how to build a Windows service here: https://riedmann.it/download/VulcanService.viaef

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Horst
Posts: 337
Joined: Tue Oct 13, 2015 3:27 pm

httpListener, AsyncCallback syntax

Post by Horst »

Hi Wolfgang
Ohh i found it in Aufgaben. I think i have Alzheimer ;-) , i am sure i asked you one year ago.
Thanks for your passion.
Horst
User avatar
Horst
Posts: 337
Joined: Tue Oct 13, 2015 3:27 pm

httpListener, AsyncCallback syntax

Post by Horst »

Hallo Wolfgang
To let it run as a task doesnt work. After about 1houre the listener stops. Now i took a look into your service example.
But i dont know how to put this 2 prg's together
Any hint ?

Horst
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

httpListener, AsyncCallback syntax

Post by wriedmann »

Hi Horst,
please look at the configuration of your task: maybe you have configured it to stop after an hour.
I have running a listener as service in a customers site, but cannot give you that code - have to build a sample.
But unfortunately I'm much under pressure these days and have not the time to spend a hour or two to build this sample.
You need somtehing like this in your ServiceMain class:

Code: Select all

	protected virtual method OnStart( args as string[] ) as void 
		
		super:OnStart( args )
		self:WriteLog( "Service was started" )
		self:StartListener()
		
		return
		
	protected virtual method OnStop() as void
		
		self:WriteLog( "Service was stopped" )
		self:StopListener()
		self:CleanUp()
		super:OnStop()
		
		return
		
	protected virtual method OnPause() as void
		
		self:WriteLog( "Service was paused" )
		self:StopListener()
		super:OnPause()
		
		return
		
	protected virtual method OnContinue() as void
		
		self:WriteLog( "Service was continued" )
		self:StartListener()
		super:OnContinue()
		
		return
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply