fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

httpListener, AsyncCallback syntax 17 Feb 2016 09:08 #164

  • wriedmann's Avatar

  • wriedmann

  • Topic Author


  • Posts: 2411
  • // 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
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    httpListener, AsyncCallback syntax 27 Oct 2018 11:21 #6617

  • Horst's Avatar

  • Horst


  • Posts: 158
  • 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.

    httpListener, AsyncCallback syntax 27 Oct 2018 11:52 #6618

  • wriedmann's Avatar

  • wriedmann

  • Topic Author


  • Posts: 2411
  • 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

    www.riedmann.it - docs.xsharp.it

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

    httpListener, AsyncCallback syntax 23 Nov 2018 16:15 #6893

  • kitz's Avatar

  • kitz


  • Posts: 37
  • 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.

    httpListener, AsyncCallback syntax 23 Nov 2018 16:22 #6894

  • wriedmann's Avatar

  • wriedmann

  • Topic Author


  • Posts: 2411
  • 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

    www.riedmann.it - docs.xsharp.it

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

    httpListener, AsyncCallback syntax 05 Feb 2020 12:31 #13067

  • Horst's Avatar

  • Horst


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

    Horst

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

    httpListener, AsyncCallback syntax 05 Feb 2020 13:15 #13068

  • wriedmann's Avatar

  • wriedmann

  • Topic Author


  • Posts: 2411
  • 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: riedmann.it/download/VulcanService.viaef

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    httpListener, AsyncCallback syntax 05 Feb 2020 14:15 #13070

  • Horst's Avatar

  • Horst


  • Posts: 158
  • 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

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

    httpListener, AsyncCallback syntax 07 Feb 2020 13:19 #13111

  • Horst's Avatar

  • Horst


  • Posts: 158
  • 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

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

    httpListener, AsyncCallback syntax 07 Feb 2020 13:32 #13112

  • wriedmann's Avatar

  • wriedmann

  • Topic Author


  • Posts: 2411
  • 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:
    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

    www.riedmann.it - docs.xsharp.it

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

    httpListener, AsyncCallback syntax 20 Feb 2020 09:44 #13334

  • Horst's Avatar

  • Horst


  • Posts: 158
  • Hallo Wolfgang
    Remember, i let run your listener as a task and every houre it stops, now i tryed to let it run with your service example as windows service. But also the service it stops sometimes. i have 2 Internet Server. On the newer one (MS Server 2019) its working without stop on the old one (MS Server 2008 R2) it stops. Now after 2h, i found a error message from the .Net Runtime

    Anwendung: xsharpservice.exe
    Frameworkversion: v4.0.30319
    Beschreibung: Der Prozess wurde aufgrund einer unbehandelten Ausnahme beendet.
    Ausnahmeinformationen: System.Net.HttpListenerException
    bei System.Net.HttpResponseStream.Write(Byte[], Int32, Int32)
    bei XsHttpListener.MainProgram.GetContextCallback(System.IAsyncResult)
    bei System.Net.LazyAsyncResult.Complete(IntPtr)
    bei System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
    bei System.Net.ListenerAsyncResult.IOCompleted(System.Net.ListenerAsyncResult, UInt32, UInt32)
    bei System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

    and then: source Applikation Error

    Name der fehlerhaften Anwendung: xsharpservice.exe, Version: 0.0.0.0, Zeitstempel: 0x5e4d0f0f
    Name des fehlerhaften Moduls: KERNELBASE.dll, Version: 6.1.7601.24545, Zeitstempel: 0x5e0eb6bd
    Ausnahmecode: 0xe0434352
    Fehleroffset: 0x000000000000b87d
    ID des fehlerhaften Prozesses: 0x6d0
    Startzeit der fehlerhaften Anwendung: 0x01d5e79ad53deb84
    Pfad der fehlerhaften Anwendung: d:\web\horst.korak\unvalid.ch\xsharpservice.exe
    Pfad des fehlerhaften Moduls: C:\Windows\system32\KERNELBASE.dll
    Berichtskennung: 1b374672-53ba-11ea-ab6e-00155dcefc02


    Can u help me ? I have no idea whats that.
    Horst

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

    httpListener, AsyncCallback syntax 20 Feb 2020 11:32 #13335

  • Horst's Avatar

  • Horst


  • Posts: 158
  • Hi
    And i try to translate this c# into x# but wtf is listener.prefixes ? its used like a array but its not.
    i tryed
    FOREACH prefix AS STRING IN _oListener:Prefixes
    oSB:AppendFormat( e"Prefixes: {0} = {1}\n", prefix) //, _oListener:Prefixes [prefix] )
    NEXT

    But the Service crashed. Btw it comes the same errormessage from .Net Runtime like the services is doing that sometimes without this part of code.

    public static void DisplayPrefixesAndState(HttpListener listener)
    {
    // List the prefixes to which the server listens.
    HttpListenerPrefixCollection prefixes = listener.Prefixes;
    foreach(string prefix in prefixes)
    {
    Console.WriteLine(prefix);
    }

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

    httpListener, AsyncCallback syntax 20 Feb 2020 11:46 #13336

  • SHirsch's Avatar

  • SHirsch


  • Posts: 173
  • Hello Horst,

    you tried to access _oListener:Prefixes like a dictionary. But it seems to be just like a list of strings.
    This should be the correct way (see {1} has no corresponding parameter in your version):
    FOREACH prefix AS STRING IN _oListener:Prefixes
    oSB:AppendFormat( e"Prefixes: {0}\n", prefix)
    NEXT

    Stefan

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

    httpListener, AsyncCallback syntax 20 Feb 2020 12:05 #13337

  • Horst's Avatar

  • Horst


  • Posts: 158
  • Hi Stefan
    Thanks that works :-)
    My fault , i did not analys what this AppendFormat is realy doing.

    Thanks

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

    httpListener, AsyncCallback syntax 20 Feb 2020 18:43 #13345

  • wriedmann's Avatar

  • wriedmann

  • Topic Author


  • Posts: 2411
  • Hi Horst,
    sorry for not having answered, but I was really busy as a new project is in production now.
    It is a production planning software, written entirely in X# and accessing data on DBFs, Oracle and PostgreSQL databases.
    But I have seen that others have helped you in the meantime - and this is the bonus of a public forum.
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    httpListener, AsyncCallback syntax 21 Sep 2020 12:45 #15922

  • kitz's Avatar

  • kitz


  • Posts: 37
  • Hello Wolfgang,
    I have a HttpListener in place and working, had nearly never problems in Win7 for wears. Recently I had to move to Win10, same program.
    Now sometimes the sender (from outside, but in company network) gets a timeout, because my listener program just stops working after a request. I don't get any error messages in my try catch blocks neither in the functions nor in the main program.
    Do you have any tip what to do to get an error message or any other hint what is going on?
    BR Kurt

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

    httpListener, AsyncCallback syntax 21 Sep 2020 13:49 #15925

  • wriedmann's Avatar

  • wriedmann

  • Topic Author


  • Posts: 2411
  • Hi Kurt,
    unfortunately I have no idea. My only production of the httpListener is running on a Windows Server 2012 R2 (it is the server version of Windows 7, now both out of maintenance).

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    httpListener, AsyncCallback syntax 21 Sep 2020 17:40 #15926

  • Horst's Avatar

  • Horst


  • Posts: 158
  • Hallo Kurt
    i used the listerner from Woffgang and every some hours it stops. Because it was only handling Get and Post, comes a HEAD request the listerner crashed. Maybe it helps.
    Horst

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

    httpListener, AsyncCallback syntax 23 Sep 2020 07:37 #15961

  • kitz's Avatar

  • kitz


  • Posts: 37
  • Hello Horst,
    thanks for the suggestion, but I know that I only get POST requests and I know the sender.
    I am testing now, if a request is finished synchronally instead of asynchronally because in that case my program would stop. Some years ago I created it, so not all details are present in my brain. Now on new computer, new OS and SSD disk it maybe fast enough…
    BR Kurt

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

    • Page:
    • 1