winform监听网页请求

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();
}


}
}

转载于:https://www.cnblogs.com/cotty/archive/2012/01/13/2321866.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值