#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Reading a remote server's POST response to my server's POST and GET commands‏


    I'm new to C#. I have some questions about POST and GET communication between two servers. My server is called "AAG." The remote server is called "LRU."

    1. I put together a WebRequest to Http POST to a remote server. I want to read the response from the remote server, which will be a POST to my server. Will a HTTP WebResponse using a StreamReader be sufficient to read the remoter server's POST response? If not, do I need to use the HttpListener to receive that POST?

    2. This question is similar to the previous. I need to implement a Http GET command to retrieve data from the remote server's .shtml page. I used a WebRequest with the GET cmd. Next, I used a StreamReader to get the page. I then used the WebResponse with a StreamReader to read the other server's
    POST response. Is this the appropriate approach to GET data from the .shtml page and read the remote server's POST response? If not, do I need to use the HttpListener to receive that POST? The attached code is what I've done.


    Thanks for any suggestions,
    -KW

    Code:
    //  generate an AAG GET to retrieve LRU's webpage.  ? Do we follow by receiving a LRU's response (which is a LRU POST)
    
            string HttpGet (string "/setFreq.shtml")
            {	
                //  generate a request from AAG to LRU server to get the LRU's webpage of selected values
                WebRequest webrequest = WebRequest.Create ("/setFreq.shtml");	// initializes AAG WebRequest instance for URI webpage location on LRU
                webRequest.Method = "GET";						// this sends GET webpage cmd to LRU server
                Stream dataStream = null;						// initialize LRU datastream for GET
                try
                {
                    dataStream = webRequest.GetResponse().GetResponseStream();	// request to get datastream from webpages
                    StreamReader sr = new StreamReader(datastream);			// reads LRU datastream
                    string sLine = "";						// initialize a line of the datastream 
    
                    // read each line of the LRU's datastream until empty
                    while (sLine != null)
                    {
                        i++;
                        sLine = sr.ReadLine();
                    }
    
                    dataStream.Close();						// close the stream
                }
    
                catch (WebException ex)						// error reading LRU's datastream
                {	
                    MessageBox.Show ( ex.Message, "httpGet:  Request error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
    	
                //  read the LRU posted response  ? will this read the LRU's POST to AAG or do I need to use a httplistener to read incoming POST?
                try
                {
                    WebResponse webResponse = webRequest.GetResponse();		// assigns response object to LRU's response to AAG's GET WebRequest
                    if(webResponse == null)
                    { return null; }
    
                    // get the LRU's datastream associated with the response object ? do i use streamreader or stream class to read data?
                    StreamReader sr = new StreamReader (webResponse.GetResponseStream());	// reads the LRU's POST response stream
                    return sr.ReadToEnd ().Trim ();						// returns read LRU's response to end of data and removes encapsulated white spaces
                }
    
                catch (WebException ex)											// error reading LRU's response
                {	
                    MessageBox.Show ( ex.Message, "httpPost:  Request error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return null;	
            }
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,128
    Rep Power
    9398
    When you GET or POST to their server, there are two possible ways the remote server responds and the words you've written don't clearly indicate which is the case:
    1. You send the request and they reply with the response
    2. You send the request, they send back nothing in the response, but they then do a separate request to your server (and you may or may not reply with a response)

    It's also possible that both happen: the remote server responds and also sends you a request, but I've never seen that happen anywhere - it's always one or the other.

    It sounds mostly like it's the first case: you write data to them and they read it, then in the same connection they write data to you and you read it. It's certainly much simpler. That's how the majority of the Internet works.

    Originally Posted by kwillie
    1. I put together a WebRequest to Http POST to a remote server. I want to read the response from the remote server, which will be a POST to my server. Will a HTTP WebResponse using a StreamReader be sufficient to read the remoter server's POST response? If not, do I need to use the HttpListener to receive that POST?
    In the case of #1, you can read the response back. In the case of #2, a HttpListener can do the job (whether you use it depends on your environment).

    Originally Posted by kwillie
    2. This question is similar to the previous. I need to implement a Http GET command to retrieve data from the remote server's .shtml page. I used a WebRequest with the GET cmd. Next, I used a StreamReader to get the page.
    Okay so far: you're describing #1.

    Originally Posted by kwillie
    I then used the WebResponse with a StreamReader to read the other server's
    POST response.
    Now you're describing #2.

    Originally Posted by kwillie
    Is this the appropriate approach to GET data from the .shtml page and read the remote server's POST response? If not, do I need to use the HttpListener to receive that POST?
    Same answer as before: if #1 then yes it's appropriate, if #2 then you need the HttpListener.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    2 servers communicating. Remote server sends only periodic POST commands.


    1st, I want to thank you for your response. I hope I understand correctly. 1st scenario: my AAG server is acting as a client and POST'ing a data command to the remote LRU server. The remote server then acts like a client and POSTs data periodically to my server. I'm not sure if this is a response to my POST or just the remote servers periodic POSTs. I think the later. So, can I read the remote server's POST to my server with either a WebResponse or an HTTPListener? How do I know if the remote server's POST is a response or a request?

    2nd scenario: my server initiates a GET command to the remote server to access data from a .shtml page. The remote server follows up with a POST of some more periodic data. This is the example code that you see. I'm not sure if I need to use a HTTPListener to read that LRU POST data or not. I think the code from lines 8 thru 23 should accurately read the data on the .shtml page. Because I suspect the remote server is really POST'ing periodic data rather than responding to my GET command, I think lines 31 thru 41 is inaccurate and should be using a HTTPListener to receive the remote server's incoming POST. Is my assumption accurate or can a WebResponse or an HTTPListener be used to read the remote server's POST?

    Also,

    This communication is based on a Wireshark data capture between two servers. My objective is to replace the old AAG server with a new one. I hope I'm describing this scenario better and understanding how the communication works.

    Thank you again for any additional clarification you can give.

IMN logo majestic logo threadwatch logo seochat tools logo