{"id":234,"date":"2016-03-05T09:52:34","date_gmt":"2016-03-05T08:52:34","guid":{"rendered":"http:\/\/www.riedmann.it\/blog\/?p=234"},"modified":"2016-03-05T09:52:34","modified_gmt":"2016-03-05T08:52:34","slug":"httplistener-class-in-vulcan-netx","status":"publish","type":"post","link":"https:\/\/blog.riedmann.it\/?p=234","title":{"rendered":"httpListener class in Vulcan.NET\/X#"},"content":{"rendered":"<p>This is code for a Vulcan.NET or X# Listener class (ported over from C#):<\/p>\n<blockquote><p>\n<code><br \/>\n\/\/ Application : HttpListener<br \/>\n\/\/ httpListener.prg , Created : 16.02.2016   08:37<br \/>\n\/\/ User : Wolfgang<br \/>\n\/\/ C# code see here:<br \/>\n\/\/ http:\/\/mikehadlow.blogspot.it\/2006\/07\/playing-with-httpsys.html<br \/>\n\/\/<br \/>\n\/\/ to make it run without admin rights you need something like this one:<br \/>\n\/\/ netsh http add urlacl url=\"http:\/\/*:8080\/\" user=[username] listen=yes<\/p>\n<p>#using System<br \/>\n#using System.Net<br \/>\n#using System.Text<br \/>\n#using System.IO<br \/>\n#using VulcanHttpListener.riedmann.it <\/p>\n<p>begin namespace VulcanHttpListener.riedmann.it <\/p>\n<p>function Start() as void<br \/>\n&nbsp;local oProgram as MainProgram<\/p>\n<p>&nbsp;oProgram := MainProgram{}<br \/>\n&nbsp;oProgram:Start()<\/p>\n<p>&nbsp;return<\/p>\n<p>class MainProgram<br \/>\n&nbsp;protect _oListener as HttpListener<\/p>\n<p>&nbsp;constructor()<\/p>\n<p>&nbsp;&nbsp;_oListener := HttpListener{}<\/p>\n<p>&nbsp;return<\/p>\n<p>&nbsp;method Start() as void<\/p>\n<p>&nbsp;&nbsp;_oListener:Prefixes:Add( \"http:\/\/*:8080\/\" )<br \/>\n&nbsp;&nbsp;_oListener:Start()<br \/>\n&nbsp;&nbsp;Console.WriteLine( \"Listening on port 8080, hit enter to stop\" )<br \/>\n&nbsp;&nbsp;_oListener:BeginGetContext( AsyncCallback{ self:GetContextCallback }, null )<br \/>\n&nbsp;&nbsp;Console.ReadLine()<br \/>\n&nbsp;&nbsp;_oListener:Stop()<\/p>\n<p>&nbsp;return<\/p>\n<p>&nbsp;method GetContextCallback( oResult as IAsyncResult ) as void<br \/>\n&nbsp;&nbsp;local oContext as HttpListenerContext<br \/>\n&nbsp;&nbsp;local oRequest as HttpListenerRequest<br \/>\n&nbsp;&nbsp;local oResponse as HttpListenerResponse<br \/>\n&nbsp;&nbsp;local oSB as StringBuilder<br \/>\n&nbsp;&nbsp;local oString as string<br \/>\n&nbsp;&nbsp;local oBuffer as byte[]<br \/>\n&nbsp;&nbsp;local oOutput as Stream<\/p>\n<p>&nbsp;&nbsp;oContext := _oListener:EndGetContext( oResult )<br \/>\n&nbsp;&nbsp;oRequest := oContext:Request<br \/>\n&nbsp;&nbsp;oResponse := oContext:Response<\/p>\n<p>&nbsp;&nbsp;oSB := StringBuilder{}<br \/>\n&nbsp;&nbsp;oSB:Append( e\"\\n\" )<br \/>\n&nbsp;&nbsp;oSB:AppendFormat( e\"HttpMethod: {0}\\n\", oRequest:HttpMethod )<br \/>\n&nbsp;&nbsp;oSB:AppendFormat( e\"URI: {0}\\n\", oRequest:Url:AbsoluteUri )<br \/>\n&nbsp;&nbsp;oSB:AppendFormat( e\"Local path: {0}\\n\", oRequest:Url:LocalPath )<br \/>\n&nbsp;&nbsp;oSB:Append( e\"\\n\" )<br \/>\n&nbsp;&nbsp;foreach cKey as string in oRequest:QueryString:Keys<br \/>\n&nbsp;&nbsp;&nbsp;oSB:AppendFormat( e\"Query: {0} = {1}\\n\", cKey, oRequest:QueryString[cKey] )<br \/>\n&nbsp;&nbsp;next<br \/>\n&nbsp;&nbsp;oSB:Append( e\"\\n\" )                                           <\/p>\n<p>&nbsp;&nbsp;oString := oSB:ToString()<br \/>\n&nbsp;&nbsp;oBuffer := System.Text.Encoding.UTF8:GetBytes( oString )<br \/>\n&nbsp;&nbsp;oResponse:ContentLength64 := oBuffer:Length<br \/>\n&nbsp;&nbsp;oOutput := oResponse:OutputStream<br \/>\n&nbsp;&nbsp;oOutput:Write( oBuffer, 0, oBuffer:Length )<\/p>\n<p>&nbsp;&nbsp;_oListener:BeginGetContext( AsyncCallback{ self, @GetContextCallback() }, null )<\/p>\n<p>&nbsp;&nbsp;return<\/p>\n<p>end class<\/p>\n<p>end namespace<br \/>\n<\/code>\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This is code for a Vulcan.NET or X# Listener class (ported over from C#): \/\/ Application : HttpListener \/\/ httpListener.prg , Created : 16.02.2016 08:37 \/\/ User : Wolfgang \/\/ C# code see here: \/\/ http:\/\/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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,23],"tags":[25,24],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-vulcan-net-programming","category-x-programming","tag-http-sys","tag-httplistener"],"_links":{"self":[{"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=234"}],"version-history":[{"count":2,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/posts\/234\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/posts\/234\/revisions\/236"}],"wp:attachment":[{"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}