• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

c# 网络编程之http

c# 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论

一、概述

本文目的是通过C#代码提供一个HTTP服务,正常情况下如果我们需要向外界提供HTTP服务,常规做法就是通过ASP.NET来实现,有时我们的应用程序或Windows服务需要向外提供一些简单的HTTP服务就可以自己实现,从而避免部署IIS增加系统复杂性。这里必须强调是一些简单的应用,如果应用比较复杂,涉及到路径解析HTML解析等,还是用WEB方式实现比较靠谱。

将HTTP和UDP、TCP放在同一个系列实际上有一点不合适,因为UDP、TCP属于传输层协议,HTTP属于应用层协议,希望读者首先有一个明确的了解。

二、 提供服务

首先启动HHTP服务:

if (!HttpListener.IsSupported)
  {
  Console.WriteLine("服务器操作系统不支持建立Http Server,需要更高版本的操作系统!");
  return;
  }

  HttpListener httpListener = new HttpListener();

  try
  {
  Console.WriteLine("正在启动Http服务");
  int port = 9000;
  ht<span>本文来源gaodai#ma#com搞*代#码9网#</span>tpListener.Prefixes.Add($"http://*:{port}/");
  httpListener.Start();  
  Console.WriteLine("Http服务启动成功。");
  }
  catch (Exception ex)
  {
  Console.WriteLine($"启动Http服务出现异常:{ex.Message}");
  return;
  }

进行监听:

while (true)
  {
  Console.WriteLine("开始监听...");
  HttpListenerContext context = httpListener.GetContext();
  HttpListenerRequest request = context.Request;

  string Method = request.HttpMethod.ToUpper();
  Console.WriteLine($"收到请求,URL:{ request.Url} Method:{Method}");  

  Response(context, "hello");
  }

代码循环进行监听,GetContext方法会引起阻塞,当收到浏览器请求时,服务器立即返回“Hello”。

Response方法实现如下:

private static void Response(HttpListenerContext context, string responseTxt)
 {
  HttpListenerResponse response = context.Response;
  response.ContentType = "html";
  response.ContentEncoding = Encoding.UTF8;

  using (Stream output = response.OutputStream)
  {
  byte[] buffer2 = Encoding.UTF8.GetBytes(responseTxt);
  output.Write(buffer2, 0, buffer2.Length);
  }
 }

此时打开浏览器输入地址 http://localhosthost:9000/ 看一下能否看到结果。(如果需要通过其他机器访问,本机要开放防火墙对应端口。)

注意:程序需要以管理员模型运行才能提供服务。

具体办法:工程新增应用程序清单文件:app.manifest,修改配置信息如下:

  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">

    <security>

      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">       

        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

      </requestedPrivileges>

    </security>

  </trustInfo>

三、 响应

通过request.HttpMethod可以取得协议类型,对于GET和POST方法将采取不同的处理方式。

通过request.RawUrl可以取得URL路径,并进行解析,通过request.QueryString可以用户输入的参数值。

if (Method == "GET")
  {
   Console.WriteLine($"Get:RawURL:{ request.RawUrl}");

   if (request.RawUrl.StartsWith("/version"))
   {
   Response(context, "Simple Http Server Ver:0.11");
   continue;
   }
   else
   {
   string username = request.QueryString["username"];
   string pwd = request.QueryString["pwd"];

   Response(context, $"Welcome:{username}");
   continue;
   }
  }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:c# 网络编程之http

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址