Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
I am a C# novice. I have primarily C coded. I have a problem that I hope someone can give me some insight and/or code snippets.

Problem: We have GUI software written in C# that communicates with LRUs (line replaceable units) for the purpose of loading and retrieving data. I have a LRU server that currently uses HTTP POST commands to transfer data to a laptop server. The laptop currently accepts that LRU POST data and displays in a web page. The laptop also uses POST and GET commands to the LRU, inorder to set and retrieve LRU values. I want to use a different laptop configured with our software to perform the same tasks. Our laptop displays the data in GUI windows and doesn’t use a web browser interface.

1. Do you have C# code examples to illustrate how our laptop might receive the following periodic HTTP POST data command from the LRU: i.e. POST /mipg3/servlet/xmlToMySql HTTP/1.0 ?

2. Do you have C# code examples to illustrate how our laptop server might send the following HTTP GET data command to the LRU: GET /setFreq.shtml HTTP/1.1 and receive the POSTed LRU response: i.e. POST /mipg3/servlet/xmlToMySql HTTP/1.0?

Below is a snippet from a Wireshark network analysis of a sample communication between the LRU and the initial laptop (using a web interface w/the LRU).

192.168.211.1 192.168.211.143 HTTP POST /mipg3/servlet/XmlToMySql Http/1.0
192.168.211.143 192.168.211.1 HTTP HTTP/1.0 200 OK
192.168.211.1 192.168.211.143 HTTP POST /mipg3/servlet/XmlToMySql Http/1.0
192.168.211.143 192.168.211.1 HTTP HTTP/1.0 200 OK
192.168.211.143 192.168.211.1 HTTP GET /setFreq.shtml HTTP/1.1
192.168.211.1 192.168.211.143 HTTP POST /mipg3/servlet/XmlToMySql HTTP/1.0

I'm not sure if the WebResponse class with GetResponseStream() is what should be used to read these incoming LRU POSTs. I'm also not sure if the WebRequest class with GetRequestStream() is what should be used to get an LRU POST.

Thank you very much for any assistance you can share,

-Kent
Posted

I would advise you to stop thinking about all those samples. Programming starts only when you start writing everything by yourself, honestly. All you need is the class System.Net.HttpWebRequest:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^].

Available Microsoft code samples are clear enough to get started.

—SA
 
Share this answer
 
v2
Comments
oodobah 8-Nov-13 18:18pm    
Thanks Sergey for your recommendations. It was very helpful. I modified the code samples as you suggested. Would you mind reading my below response to Ranjan? I'm alittle fuzzy still on handling a POST response to my POST. I'm also fuzzy on implementing the GET cmd to get data from a .shtml page. Again, your suggestions are very welcome.

Thanks,

-Kent
1. You need to have HttpListener to receive incoming LRU POST data.


http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx[^]

http://stackoverflow.com/questions/8637856/httplistener-with-post-data[^]

2. You can easily do a HTTP Web Request with WebRequest or go with HttpWebRequest as "Sergey Alexandrovich Kryukov" said.

http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[^]
 
Share this answer
 
Comments
oodobah 8-Nov-13 18:11pm    
Thank you for your response.

1. I put together a WebRequest to POST to a remote server. If I want to read the response from the 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'd response, or do I need to use the HttpListerner to receive that?

2. I also implemented the HttpListener to receive periodic POSTs from the remote LRU server.

3. I need to implement and Http Get command to retrieve data from the remote server's .shtml page. I tried using a WebRequest with the GET cmd. I then used a StreamReader to get the page.

Below code is what I've done. Is this the appropriate approach to GET data from the .shtml page? Thanks again for any suggestions, -Kent
//  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?
            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 answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900