如何使用 HttpListener 接收包含 XML 的 HTTP Post

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7004616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:52:57  来源:igfitidea点击:

how to use HttpListener to receive HTTP Post which contain XML

xmlhttpposthttplistener

提问by briswill

I am working on a project that will receive HTTP POSTs which contain XML data. I am going to set up HttpListener to receive HTTP POST and then response with ACK.

我正在开发一个项目,该项目将接收包含 XML 数据的 HTTP POST。我将设置 HttpListener 以接收 HTTP POST,然后使用 ACK 进行响应。

I am wondering if there are any examples that implement similar functionality? And how many requests could HttpListener handle in the same time?

我想知道是否有任何实现类似功能的示例?HttpListener 可以同时处理多少个请求?

I will have a message queue to store the requests from the client. And I will have to set up a test client to send the request to the HttpListener for testing purposes. Should I set up a WebRequest or something else to test HttpListener?

我将有一个消息队列来存储来自客户端的请求。我将不得不设置一个测试客户端以将请求发送到 HttpListener 以进行测试。我应该设置一个 WebRequest 还是其他东西来测试 HttpListener?

回答by Bryan Rehbein

You can use HttpListener to process incoming HTTP POSTs, you can pretty much follow any tutorial you find for the listener. Here is how I am doing it (note this is syncronous, to handle more than 1 request at a time, you will want to use threads or at least the async methods.)

您可以使用 HttpListener 处理传入的 HTTP POST,您几乎可以按照为侦听器找到的任何教程进行操作。这是我的做法(请注意,这是同步的,要一次处理 1 个以上的请求,您将需要使用线程或至少使用异步方法。)

public void RunServer()
{
    var prefix = "http://*:4333/";
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add(prefix);
    try
    {
        listener.Start();
    }
    catch (HttpListenerException hlex)
    {
        return;
    }
    while (listener.IsListening)
    {
        var context = listener.GetContext();
        ProcessRequest(context);
    }
    listener.Close();
}

private void ProcessRequest(HttpListenerContext context) 
{
    // Get the data from the HTTP stream
    var body = new StreamReader(context.Request.InputStream).ReadToEnd();

    byte[] b = Encoding.UTF8.GetBytes("ACK");
    context.Response.StatusCode = 200;
    context.Response.KeepAlive = false;
    context.Response.ContentLength64 = b.Length;

    var output = context.Response.OutputStream;
    output.Write(b, 0, b.Length);
    context.Response.Close();
}

The main part that gets the XML from the request is this line:

从请求中获取 XML 的主要部分是这一行:

var body = new StreamReader(context.Request.InputStream).ReadToEnd();

This give you the body of the HTTP request, which should contain your XML. You could probably send it straight into any XML library that can read from a stream, but be sure to watch for exceptions if a stray HTTP request also gets sent to your server.

这将为您提供 HTTP 请求的正文,其中应包含您的 XML。您可能可以将它直接发送到任何可以从流中读取的 XML 库中,但是如果一个杂散的 HTTP 请求也被发送到您的服务器,请务必注意异常。