fbpx

httpListener, AsyncCallback syntax

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
2 years 9 months ago #1 by wriedmann
httpListener, AsyncCallback syntax was created by wriedmann
// C# code see here:
// mikehadlow.blogspot.it/2006/07/playing-with-httpsys.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

Please Log in or Create an account to join the conversation.

More
1 month 2 weeks ago #2 by Horst
Replied by Horst on topic httpListener, AsyncCallback syntax
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

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 month 2 weeks ago #3 by wriedmann
Replied by wriedmann on topic httpListener, AsyncCallback syntax
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

Please Log in or Create an account to join the conversation.

More
2 weeks 5 days ago #4 by kitz
Replied by kitz on topic httpListener, AsyncCallback syntax
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

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
2 weeks 5 days ago #5 by wriedmann
Replied by wriedmann on topic httpListener, AsyncCallback syntax
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

Please Log in or Create an account to join the conversation.