0

我是 C# 新手。我主要用 C 编码。我有一个问题,希望有人能给我一些见解和/或代码片段。

问题:我们有用 C# 编写的 GUI 软件,它与 LRU(行可替换单元)通信以加载和检索数据。我有一个 LRU 服务器,它当前使用 HTTP POST 命令将数据传输到笔记本电脑服务器。笔记本电脑当前接受 LRU POST 数据并显示在网页中。膝上型电脑还对 LRU 使用 POST 和 GET 命令,以便设置和检索 LRU 值。我想使用配置了我们软件的不同笔记本电脑来执行相同的任务。我们的笔记本电脑在 GUI 窗口中显示数据,并且不使用 Web 浏览器界面。

  1. 您是否有 C# 代码示例来说明我们的笔记本电脑如何从 LRU 接收以下定期 HTTP POST 数据命令:即 POST /mipg3/servlet/xmlToMySql HTTP/1.0?

  2. 您是否有 C# 代码示例来说明我们的笔记本电脑服务器如何向 LRU 发送以下 HTTP GET 数据命令:GET /setFreq.shtml HTTP/1.1 并接收 POSTed LRU 响应:即 POST /mipg3/servlet/xmlToMySql HTTP/1.0 ?

下面是 Wireshark 网络分析的一个片段,它对 LRU 和初始笔记本电脑之间的示例通信进行了分析(使用带有 LRU 的 Web 界面)。

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

我不确定是否应该使用带有 GetResponseStream() 的 WebResponse 类来读取这些传入的 LRU POST。我也不确定是否应该使用带有 GetRequestStream() 的 WebRequest 类来获取 LRU POST。

非常感谢您可以分享的任何帮助,

-肯特

ps

下面是可用于 POST 到 LRU 服务器并接收响应的代码。我不确定如何实现接收传入的 POST 和提交 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 
4

0 回答 0