C# 如何在C中同时运行HTTP POST和HTTPListener#
我是个新手,希望有人能帮我。我有两个设备作为客户机-服务器或服务器-客户机对。这取决于谁正在向其他设备执行HTTP POST。当设备充当客户端时,我正在实现HTTP POST代码。我还实现了HTTPListener代码,用于 该设备正在充当服务器 我希望HTTPListener在定期发布时与HTTP POST代码并发运行。我不知道这是怎么回事 HTTPListener侦听传入的帖子。它似乎使程序等待,直到识别出首选URI。然后它出现了 接受其他设备的请求并发布数据 我应该将HTTPListener放在程序执行期间随时触发的事件处理程序中吗?我相信这会 暂停当前程序的执行以执行HTTPListener代码。侦听器代码完成后,程序 应该在最初停止的地方继续。我理解正确吗 下面是我的一些代码的简化说明。我希望它相当清楚。感谢您对HTTP和事件概念的帮助,任何人都可以分享C# 如何在C中同时运行HTTP POST和HTTPListener#,c#,http,events,C#,Http,Events,我是个新手,希望有人能帮我。我有两个设备作为客户机-服务器或服务器-客户机对。这取决于谁正在向其他设备执行HTTP POST。当设备充当客户端时,我正在实现HTTP POST代码。我还实现了HTTPListener代码,用于 该设备正在充当服务器 我希望HTTPListener在定期发布时与HTTP POST代码并发运行。我不知道这是怎么回事 HTTPListener侦听传入的帖子。它似乎使程序等待,直到识别出首选URI。然后它出现了 接受其他设备的请求并发布数据 我应该将HTTPListene
// this is my primary class from which all program execution is to be controlled from
public class Lru_operation
{
#region fields
// contains field members used within LruOperation()
#endregion fields
#region Properties
public Lru_Listen LruListen = new Lru_Listen(); // create Lru_Listen object for accessing another class method(s)
public Lru_Post HttpLruPost = new Lru_Post(); // same purpose as above
#endregion Properties
[STAThread]
static void Main()
{
Lru_operation LruOpX = new Lru_operation(); // create a Lru_operation object
LruOpX.LruOperation(); // call to LruOperation()
}
public void LruOperation()
{
LruListen.ListenForAag(); // calls a method from the Lru_Listen class that starts the HTTPListener
try
{
while(true)
{
// performs further code tasks with methods, etc. from other source files
HttpLruPost.LruHttpPost(AagLocation, ChanDet.DropSondeData); // performs the HTTP POSTs to the other device
// more tasks
//IMPORTANT***Should I remove the below code and encapsulate the below method within an event handler?
LruListen.LruListenAccReq(); // performs the tasks of accessing the other devices webRequest and POST data
}
}
catch(Exception ex)
{
// performs other tasks
}
}
}
// this class handles listening for incoming HTTP POSTs from the other device
public class Lru_Listen
{
// this method identifies the preferred URI to listen for and starts the listener
public void ListenForAag()
{
string prefix = "http://cgi-gin/setFreq/";
if (!HttpListener.IsSupported)
{
//throw Exception;
}
if(prefix == null || prefix.Length == 0)
{
//throw new argumentException("prefix");
}
listener = new HttpListener();
listener.Prefixes.Add(prefix);
listener.Start();
// this method handles the access of the other device's HTTP POST msg and responds back
public void LruListenAccReq()
{
// synchronous: Getcontext method blocks other requests while waiting for an Aag POST cmd msg request
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
LruShowRequestData(request); // reads the other devices POST'ed data
// responds back to other device
}
}
// this class handles this device's HTTP POSTs to the other device and receives the response
public class Lru_Post
{
// this method handles the request and sending of POST data to the other device
public string LruHttpPost(string AagMsgLocation, string dropSondeMsg)
{
WebRequest webRequest = WebRequest.Create(AagMsgLocation);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes (dropSondeMsg);
Stream dataStream = null;
webRequest.ContentLength = bytes.Length;
dataStream = webRequest.GetRequestStream();
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Close();
// get the response from the other device
}
}
您可以查看WebRequest.GetRequestStreamAsync之类的内容。但是,你可能想看看HttpClient,让事情变得简单一点。我会研究一下。听起来事件和事件处理程序在这里不适用。谢谢你,彼得。