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.
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 ?
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
p.s.
Below is code that can be used to POST to the LRU server and receive a response. I'm not sure how to implement for receiving incoming POSTs and submitting a GET.
// Test code: HTTP POST w/return response string
using System.Net;
...
string HttpPost (string uri, string parameters)
{
// parameters: name1=value1&name2=value2
WebRequest webRequest = WebRequest.Create (uri);
//string ProxyString =
// System.Configuration.ConfigurationManager.AppSettings
// [GetConfigKey("proxy")];
//webRequest.Proxy = new WebProxy (ProxyString, true);
//Commenting out above required change to App.Config
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes (parameters);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; //Count bytes to send
os = webRequest.GetRequestStream();
os.Write (bytes, 0, bytes.Length); //Send it
}
catch (WebException ex)
{
MessageBox.Show ( ex.Message, "HttpPost: Request error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
}
finally
{
if (os != null)
{
os.Close();
}
}
try
{ // get the response
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{ return null; }
StreamReader sr = new StreamReader (webResponse.GetResponseStream());
return sr.ReadToEnd ().Trim ();
}
catch (WebException ex)
{
MessageBox.Show ( ex.Message, "HttpPost: Response error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
}
return null;
} // end HttpPost