向远程服务器发送HTTP GET命令和接收POST命令 [英] Sending HTTP GET commands and Receiving POST commands to/from a remote server

查看:166
本文介绍了向远程服务器发送HTTP GET命令和接收POST命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



2.您是否有C#代码示例来说明我们的笔记本电脑服务器如何将以下HTTP GET数据命令发送到LRU: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。



非常感谢您提供任何帮助分享,



-Kent

解决方案

我建​​议你不要再考虑所有这些样本了。只有当你开始自己开始编写所有内容时,编程才会开始。所有你需要的是类 System.Net.HttpWebRequest

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



可用的Microsoft代码示例足够清晰。



-SA


< BLOCKQUOTE> 1。您需要让HttpListener接收传入的LRU POST数据。





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



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



2.您可以轻松地使用WebRequest执行HTTP Web请求,或者使用HttpWebRequest作为Sergey Alexandrovich Kryukov说。



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


 //生成AAG GET以检索LRU的网页。 ?我们是否接收到LRU的响应(这是一个LRU POST)
字符串HttpGet(字符串/setFreq.shtml)
{
//生成从AAG到LRU服务器的请求获取LRU的选定值网页
WebRequest webrequest = WebRequest.Create(/ setFreq.shtml); //为LRU上的URI网页位置初始化AAG WebRequest实例
webRequest.Method =GET; //这将GET网页cmd发送到LRU服务器
Stream dataStream = null; //为GET
初始化LRU数据流尝试
{
dataStream = webRequest.GetResponse()。GetResponseStream(); //请求从网页获取数据流
StreamReader sr = new StreamReader(datastream); //读取LRU数据流
string sLine =; //初始化数据流的一行
//读取LRU数据流的每一行,直到空
而(sLine!= null)
{
i ++;
sLine = sr.ReadLine();
}
//dataStream.Close(); //关闭流
}
catch(WebException ex)//错误读取LRU的数据流
{
MessageBox.Show(ex.Message,httpGet:Request error,
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
//阅读LRU发布的回复?这会读到LRU对AAG的POST吗?
try
{
WebResponse webResponse = webRequest.GetResponse(); //将响应对象分配给LRU对AAG的GET WebRequest
的响应if(webResponse == null)
{return null; }
//获取与响应对象关联的LRU数据流?我使用streamreader或流类来读取数据?
StreamReader sr = new StreamReader(webResponse.GetResponseStream()); //读取LRU的POST响应流
return sr.ReadToEnd()。Trim(); //返回读取LRU对数据结尾的响应并删除封装的空格
}
catch(WebException ex)//错误读取LRU的响应
{
MessageBox.Show(ex。消息,httpPost:请求错误,
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
返回null;
}


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

解决方案

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


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[^]


//  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;	
        }


这篇关于向远程服务器发送HTTP GET命令和接收POST命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆