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

Discussion in 'C#' started by kwillie, Nov 14, 2013.

  1. kwillie

    kwillie New Member

    Joined:
    Nov 12, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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 alot 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;	
            }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice