你可以使用HttpListener來處理傳入的HTTP POST,你幾乎可以按照你找到的任何教程來監聽。這裏是我正在做它(注意這是syncronous,在一次處理超過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();
}
是可以獲得的主要部分來自請求的XML是這一行:
var body = new StreamReader(context.Request.InputStream).ReadToEnd();
這給你HTTP請求的主體,它應該包含你的XML。您可以直接將它發送到任何可以從流中讀取的XML庫,但是如果發送了一個流氓HTTP請求也發送到您的服務器,請務必觀察異常。