zoukankan      html  css  js  c++  java
  • HttpListener 监听HTTP 请求

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;

    namespace ConsoleAppHttpListener
    {
    /// <summary>
    /// 监听HTTP 请求 By Rhythmk
    /// </summary>
    class Program
    {
    static void Main(string[] args)
    {
    ListenerHttpUrl();

    Console.ReadKey();
    }

    static void ListenerHttpUrl()
    {
    string url = "http://admin2.rhytmk.t/rhythmk/";
    using(HttpListener hListener=new HttpListener ())
    {
    // 匿名访问
    hListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
    hListener.Prefixes.Add(url);
    //hListener.Prefixes.Add("url2")
    hListener.Start();
    Console.WriteLine(" ---Start---- ");
    while (true)
    {
    HttpListenerContext hlContext = hListener.GetContext();
    hlContext.Response.StatusCode = 200;
    string Key = hlContext.Request.QueryString["key"];
    if (!string.IsNullOrEmpty(Key))
    {
    Console.WriteLine(string.Format("The key is {0},Time : {1}",Key,DateTime.Now));
    }
    // 关闭请求
    hlContext.Response.Close();

    // 输出响应HTML 结构
    //using (StreamWriter writer = new StreamWriter(hlContext.Response.OutputStream))
    //{
    // writer.WriteLine("true");
    // writer.Close();
    // hlContext.Response.Close();
    //}
    }
    }
    }

    }
    }
  • 相关阅读:
    函数
    字符编码和文件处理
    内置方法
    day6课后复习
    第四十篇、美颜篇
    第三十八篇、给UITabBar按钮的动画效果
    第三十九篇、NavBar动态隐藏、设置透明、毛玻璃效果
    第九篇、Swift的基本使用
    第三十七篇、毛玻璃效果
    第三十六篇、webService
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2284671.html
Copyright © 2011-2022 走看看