当前位置: 首页>>代码示例>>C#>>正文


C# HttpListener.GetContext方法代码示例

本文整理汇总了C#中System.Net.HttpListener.GetContext方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListener.GetContext方法的具体用法?C# HttpListener.GetContext怎么用?C# HttpListener.GetContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.HttpListener的用法示例。


在下文中一共展示了HttpListener.GetContext方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SimpleListenerExample

// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
    if (!HttpListener.IsSupported)
    {
        Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
        return;
    }
    // URI prefixes are required,
    // for example "http://contoso.com:8080/index/".
    if (prefixes == null || prefixes.Length == 0)
      throw new ArgumentException("prefixes");
    
    // Create a listener.
    HttpListener listener = new HttpListener();
    // Add the prefixes.
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    Console.WriteLine("Listening...");
    // Note: The GetContext method blocks while waiting for a request. 
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    // Obtain a response object.
    HttpListenerResponse response = context.Response;
    // Construct a response.
    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length;
    System.IO.Stream output = response.OutputStream;
    output.Write(buffer,0,buffer.Length);
    // You must close the output stream.
    output.Close();
    listener.Stop();
}
开发者ID:.NET开发者,项目名称:System.Net,代码行数:38,代码来源:HttpListener.GetContext

示例2: HttpListener.GetContext()

//引入命名空间
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Xml;

public class MainClass
{
    public static void Main()
    {
        using (HttpListener listener = new HttpListener())
        {
            listener.AuthenticationSchemes = AuthenticationSchemes.Negotiate;
            listener.Prefixes.Add("http://localhost:8080/");
            listener.Prefixes.Add("https://localhost/");
            listener.Start();

            HttpListenerContext ctx = listener.GetContext();
            ctx.Response.StatusCode = 200; 
            string name = ctx.Request.QueryString["name"];

            StreamWriter writer = new StreamWriter(ctx.Response.OutputStream);
            writer.WriteLine("<P>Hello, {0}</P>", name);
            
            writer.WriteLine("<ul>");
            foreach (string header in ctx.Request.Headers.Keys)
            {
                writer.WriteLine("<li><b>{0}:</b> {1}</li>",header, ctx.Request.Headers[header]);
            }
            writer.WriteLine("</ul>");

            writer.Close();
            ctx.Response.Close();
            listener.Stop();
        }
    }
}
开发者ID:C#程序员,项目名称:System.Net,代码行数:45,代码来源:HttpListener.GetContext


注:本文中的System.Net.HttpListener.GetContext方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。