Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   How to run HTTP POSTs and HTTPListener concurrently in C# (http://www.go4expert.com/forums/run-http-posts-httplistener-t29941/)

kwillie 24Dec2013 02:44

How to run HTTP POSTs and HTTPListener concurrently in C#
I'm a C# novice and hoping someone can help me. I have two devices that act as either a client-server or server-client pair. It depends upon who is performing HTTP POSTs to the other device. I'm implementing HTTP POST code for when the device is acting as a client. I'm also implementing HTTPListener code for when the device is acting as the server.

I'd like the HTTPListener to run concurrently with the HTTP POST code while it is making periodic POSTs. I'm not sure how the HTTPListener listens for incoming POSTs. It appears to make the program wait until a preferred URI is recognized. It then appears to accept the other device's request and POST data.

Should I put the HTTPListener within an event handler that is triggered anytime during program execution? I believe this would pause the current program execution inorder to execute the HTTPListener code. After the listener code is completed, the program should resume where it intially left off. Do I understand this correctly?

Below is some of my code simplified to illustrate. I hope it's fairly clear. Thank you for assistance with HTTP and event concepts anyone can share.


// this is my primary class from which all program execution is to be controlled from
                        public class Lru_operation
                                #region fields
                                // contains field members used within LruOperation()
                                #endregion fields

                                #region Properties

                                public Lru_Listen LruListen = new Lru_Listen();                // create Lru_Listen object for accessing another class method(s)
                                public Lru_Post HttpLruPost = new Lru_Post();                // same purpose as above
                                #endregion Properties

                                static void Main()
                                        Lru_operation LruOpX = new Lru_operation();                // create a Lru_operation object       
                                        LruOpX.LruOperation();                                        // call to LruOperation()

                                public void LruOperation()
                                        LruListen.ListenForAag();        // calls a method from the Lru_Listen class that starts the HTTPListener

                                                        // performs further code tasks with methods, etc. from other source files
                                                        HttpLruPost.LruHttpPost(AagLocation, ChanDet.DropSondeData);  // performs the HTTP POSTs to the other device

                                                        // more tasks

                                                        //IMPORTANT***Should I remove the below code and encapsulate the below method within an event handler?
                                                        LruListen.LruListenAccReq();        // performs the tasks of accessing the other devices webRequest and POST data
                                        catch(Exception ex)
                                                // performs other tasks


// this class handles listening for incoming HTTP POSTs from the other device
                        public class Lru_Listen
                                // this method identifies the preferred URI to listen for and starts the listener
                                public void ListenForAag()
                                        string prefix = UriLocation;
                                        if (!HttpListener.IsSupported)                                                       
                                                //throw Exception;

                                if(prefix == null || prefix.Length == 0)                               
                                        //throw new argumentException("prefix");

                                listener = new HttpListener();                               


                                // this method handles the access of the other device's HTTP POST msg and responds back
                                public void LruListenAccReq()
                                        // synchronous: Getcontext method blocks other requests while waiting for an Aag POST cmd msg request

                                        HttpListenerContext context = listener.GetContext();       
                                        HttpListenerRequest request = context.Request;               

                                        LruShowRequestData(request);                // reads the other devices POST'ed data

                                        // responds back to other device


// this class handles this device's HTTP POSTs to the other device and receives the response
                        public class Lru_Post
                                // this method handles the request and sending of POST data to the other device
                                public string LruHttpPost(string AagMsgLocation, string dropSondeMsg)               
                                        WebRequest webRequest = WebRequest.Create(AagMsgLocation);
      // assign a webRequest ContentType               
                                                                                                            webRequest.Method = "POST";                                                                               
                                        byte[] bytes = Encoding.ASCII.GetBytes (dropSondeMsg);                       
                                        Stream dataStream = null;                                                                               

                                        webRequest.ContentLength = bytes.Length;                                       
                                        dataStream = webRequest.GetRequestStream();                               
                                        dataStream.Write(bytes, 0, bytes.Length);                       

                                        // get the response from the other device                                                                                       

All times are GMT +5.5. The time now is 17:34.