如何在.NET中接收发布数据(XML),如java servlet doPost

时间:2023-01-22 01:11:15

I want to receive the HTTP POST data(XML), the XML data post by other webServer(Tomcat, shttpd).

我希望收到HTTP POST数据(XML),其他webServer(Tomcat,shttpd)的XML数据发布。

On Java I can use servlet doPost receive post data, I'm a newbie on C#, I don't know how to write it on c#.

在Java上我可以使用servlet doPost接收post数据,我是C#的新手,我不知道如何在c#上编写它。

4 个解决方案

#1


In .NET, network data communications is covered by WCF.

在.NET中,WCF涵盖网络数据通信。

Windows Communication Foundation

Windows Communication Foundation

Windows Communication Foundation (WCF) is Microsoft’s unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and inter-operate with existing investments.

Windows Communication Foundation(WCF)是Microsoft用于构建面向服务的应用程序的统一编程模型。它使开发人员能够构建安全,可靠,交易的解决方案,这些解决方案可以跨平台集成并与现有投资互操作。

For information on consuming Web Services, see Building Windows Communication Foundation Clients.

有关使用Web服务的信息,请参阅构建Windows Communication Foundation客户端。

#2


I wrote a simple receive post data use System.Net.HttpListener Class, but this only receive once and exit programming.

我写了一个简单的接收帖子数据使用System.Net.HttpListener Class,但这只接收一次并退出编程。

How can I recive more.

我怎么能更多地回忆

Thinks.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

/*
 * By: Bian Jiang
 * Blog: http://wifihack.net
 * 
 */

public class SimpleLinsstener
{

    public static void ShowRequestData(HttpListenerRequest request)
    {
        if (!request.HasEntityBody)
        {
            Console.WriteLine("No client data was sent with the request.");
            return;
        }
        System.IO.Stream body = request.InputStream;
        System.Text.Encoding encoding = request.ContentEncoding;
        System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
        if (request.ContentType != null)
        {
            Console.WriteLine("Client data content type {0}", request.ContentType);
        }
        Console.WriteLine("Client data content length {0}", request.ContentLength64);

        Console.WriteLine("Start of client data:");
        // Convert the data to a string and display it on the console.
        string s = reader.ReadToEnd();
        Console.WriteLine(s);
        Console.WriteLine("End of client data:");
        body.Close();
        reader.Close();
        // If you are finished with the request, it should be closed also.
    }


    // This example requires the System and System.Net namespaces.
    public static void SimpleListenerExample(string prefixes)
    {
        if (!HttpListener.IsSupported)
        {
            Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
            return;
        }
        // URI prefixes are required,
        // for example "http://contoso.com:8080/index/".
        if (prefixes == null)
            throw new ArgumentException("prefixes");

        // Create a listener.
        HttpListener listener = new HttpListener();
        // Add the prefixes.
        //foreach (string s in prefixes)
        //{
        listener.Prefixes.Add(prefixes);
        //}
        listener.Start();
        Console.WriteLine("Listening...");
        // Note: The GetContext method blocks while waiting for a request. 
        HttpListenerContext context = listener.GetContext();

        HttpListenerRequest request = context.Request;
        ShowRequestData(request);


        // Obtain a response object.
        HttpListenerResponse response = context.Response;
        // Construct a response.
        string responseString = "ok";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        // You must close the output stream.
        output.Close();

        listener.Stop();
    }


    static void Main()
    {
        // Write to console
        Console.WriteLine("Welcome to the C# Station Tutorial!");
        string[] strUserNames = new String[1] {"http://*:8080/Receive/" };
        SimpleListenerExample("http://*:8080/Receive/");
    }

}

#3


while(true)
{
   listener.Start();
        Console.WriteLine("Listening...");
        // Note: The GetContext method blocks while waiting for a request. 
        HttpListenerContext context = listener.GetContext();

        HttpListenerRequest request = context.Request;
        ShowRequestData(request);


        // Obtain a response object.
        HttpListenerResponse response = context.Response;
        // Construct a response.
        string responseString = "ok";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        // You must close the output stream.
        output.Close();

        listener.Stop();

}

#4


See "Walkthrough: Creating a Synchronous HTTP Handler".

请参阅“演练:创建同步HTTP处理程序”。

#1


In .NET, network data communications is covered by WCF.

在.NET中,WCF涵盖网络数据通信。

Windows Communication Foundation

Windows Communication Foundation

Windows Communication Foundation (WCF) is Microsoft’s unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and inter-operate with existing investments.

Windows Communication Foundation(WCF)是Microsoft用于构建面向服务的应用程序的统一编程模型。它使开发人员能够构建安全,可靠,交易的解决方案,这些解决方案可以跨平台集成并与现有投资互操作。

For information on consuming Web Services, see Building Windows Communication Foundation Clients.

有关使用Web服务的信息,请参阅构建Windows Communication Foundation客户端。

#2


I wrote a simple receive post data use System.Net.HttpListener Class, but this only receive once and exit programming.

我写了一个简单的接收帖子数据使用System.Net.HttpListener Class,但这只接收一次并退出编程。

How can I recive more.

我怎么能更多地回忆

Thinks.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

/*
 * By: Bian Jiang
 * Blog: http://wifihack.net
 * 
 */

public class SimpleLinsstener
{

    public static void ShowRequestData(HttpListenerRequest request)
    {
        if (!request.HasEntityBody)
        {
            Console.WriteLine("No client data was sent with the request.");
            return;
        }
        System.IO.Stream body = request.InputStream;
        System.Text.Encoding encoding = request.ContentEncoding;
        System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
        if (request.ContentType != null)
        {
            Console.WriteLine("Client data content type {0}", request.ContentType);
        }
        Console.WriteLine("Client data content length {0}", request.ContentLength64);

        Console.WriteLine("Start of client data:");
        // Convert the data to a string and display it on the console.
        string s = reader.ReadToEnd();
        Console.WriteLine(s);
        Console.WriteLine("End of client data:");
        body.Close();
        reader.Close();
        // If you are finished with the request, it should be closed also.
    }


    // This example requires the System and System.Net namespaces.
    public static void SimpleListenerExample(string prefixes)
    {
        if (!HttpListener.IsSupported)
        {
            Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
            return;
        }
        // URI prefixes are required,
        // for example "http://contoso.com:8080/index/".
        if (prefixes == null)
            throw new ArgumentException("prefixes");

        // Create a listener.
        HttpListener listener = new HttpListener();
        // Add the prefixes.
        //foreach (string s in prefixes)
        //{
        listener.Prefixes.Add(prefixes);
        //}
        listener.Start();
        Console.WriteLine("Listening...");
        // Note: The GetContext method blocks while waiting for a request. 
        HttpListenerContext context = listener.GetContext();

        HttpListenerRequest request = context.Request;
        ShowRequestData(request);


        // Obtain a response object.
        HttpListenerResponse response = context.Response;
        // Construct a response.
        string responseString = "ok";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        // You must close the output stream.
        output.Close();

        listener.Stop();
    }


    static void Main()
    {
        // Write to console
        Console.WriteLine("Welcome to the C# Station Tutorial!");
        string[] strUserNames = new String[1] {"http://*:8080/Receive/" };
        SimpleListenerExample("http://*:8080/Receive/");
    }

}

#3


while(true)
{
   listener.Start();
        Console.WriteLine("Listening...");
        // Note: The GetContext method blocks while waiting for a request. 
        HttpListenerContext context = listener.GetContext();

        HttpListenerRequest request = context.Request;
        ShowRequestData(request);


        // Obtain a response object.
        HttpListenerResponse response = context.Response;
        // Construct a response.
        string responseString = "ok";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        // You must close the output stream.
        output.Close();

        listener.Stop();

}

#4


See "Walkthrough: Creating a Synchronous HTTP Handler".

请参阅“演练:创建同步HTTP处理程序”。