-
大小: 3KB文件类型: .7z金币: 1下载: 1 次发布日期: 2021-05-14
- 语言: C#
- 标签: httplistener 高并发 C#
资源简介
C#调用httplistener实现简单的http服务器例子:编译后是一个控制台应用程序,启动后,可通过 http://127.0.0.1/ 访问,采用了回调模式提供http服务,支持高并发
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Web;
namespace ConsoleApplication1
{
class Program
{
static HttpListener sSocket = null;
public static string modulePath;
static void Main(string[] args)
{
sSocket = new HttpListener();
sSocket.Prefixes.Add(“http://127.0.0.1:80/“);
sSocket.Start();
sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack) sSocket);
modulePath = System.AppDomain.CurrentDomain.baseDirectory;
Console.WriteLine(“modulePath : “+modulePath+“\n“);
Console.Read();
}
static void GetContextCallBack(IAsyncResult ar)
{
try
{
sSocket = ar.AsyncState as HttpListener;
HttpListenerContext context = sSocket.EndGetContext(ar);
sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack) sSocket);
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
Console.WriteLine(request.Url.PathAndQuery);
string username = HttpUtility.ParseQueryString(request.Url.Query).Get(“username“);
string pwd = HttpUtility.ParseQueryString(request.Url.Query).Get(“pwd“);
string absPath = request.Url.AbsolutePath.Substring(1);
Console.WriteLine(“absPath:“+absPath);
if (absPath == “favicon.ico“)
{
string filename=Path.Combine(modulePathabsPath);
if(File.Exists(filename))
{
try
{
Console.WriteLine(“Process ico“);
Stream input = new FileStream(filename FileMode.Open);
string mime = “image/x-icon“;
response.ContentType = mime;
response.ContentLength64 = input.Length;
response.AddHeader(“Date“ DateTime.Now.ToString(“r“));
response.AddHeader(“Last-Modified“ File.GetLastWriteTime(filename).ToString(“r“));
byte[] obuffer = new byte[1024 * 32];
int nbytes;
while ((nbytes = input.Read(obuffer 0 obuffer.Length)) > 0)
{
response.OutputStream.Write(obuffer 0 nbytes);
}
input.Close();
response.OutputStream.Flush();
response.StatusCode = (int)HttpStatusCode.OK;
obuff
相关资源
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
- C# 使用ListView控件实现图片浏览器(源
- C#简单窗体聊天程序
- C#指纹识别系统程序 报告
- c# 高校档案信息管理系统
评论
共有 条评论