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


    }
    }

  • 相关阅读:
    如何升级 sof_to_rbf.bat 文件
    用sopc-create-header-files工具产生头文件提示找不到命令
    HTTP状态码
    HTML5与CSS3知识点总结
    uni-app input 监听回车键 输入回车确定
    原生微信小程序转换uni-app
    关闭vscode保存就自动格式化的功能
    uni-appH5(uni.chooseFile uni.chooseImage)限制图片类型
    uni-appH5(uni.chooseFile uni.chooseImage)上传图片大小限制大小
    textRNN & textCNN
  • 原文地址:https://www.cnblogs.com/cotty/p/2321866.html
Copyright © 2020-2023  润新知