using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Xml.Linq;
namespace WindowsFormService
{
public class LFHttpListener
{
public void listener()
{
// System.Console.WriteLine(System.Net.HttpListener.IsSupported ? "可用于本系统" : "不可用于本系统");
if (!HttpListener.IsSupported)
{
MessageBox.Show("本系统只支持xp sp2/win2003");
return;
}
System.Net.HttpListener httplistener = new System.Net.HttpListener();
string s = string.Format(XMLTools.getXmlValue("LCListenerAddress","Address"));
httplistener.Prefixes.Add(s);
httplistener.Start();
while (true)
{
System.IAsyncResult ia2 = httplistener.BeginGetContext(
delegate(System.IAsyncResult ia)
{
acceptAndResponse(ia);
},
httplistener
);
System.Threading.Thread.Sleep(1000);
}
}
private void acceptAndResponse(IAsyncResult ia)
{
//接收
var request = (HttpListener)ia.AsyncState;
var hlc = request.EndGetContext(ia);
XDocument xdoc = new XDocument();
StreamReader inputStream = new StreamReader(hlc.Request.InputStream);
string xml = inputStream.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
//XmlDocument转换成XDocument
xdoc = doc.ToXDocument();
//把接收到的XML传递处理,并返回响应XML
var acceptAndSendXml = new AcceptAndSendXml();
var reXml = acceptAndSendXml.AcceptXml(xdoc);
//XDocument转换成XmlDocument
// XmlDoc.LoadXml(XDoc.Document.ToString());
//响应XML
hlc.Response.ContentType = "text/xml";
System.IO.StreamWriter sw = new System.IO.StreamWriter(
hlc.Response.OutputStream,
System.Text.Encoding.Default
);
sw.Write(reXml);
sw.Flush();
sw.Close();
}
}
}