当前位置: 移动技术网 > IT编程>开发语言>.net > C#实现HTTP协议迷你服务器(两种方法)

C#实现HTTP协议迷你服务器(两种方法)

2017年12月12日  | 移动技术网IT编程  | 我要评论

代号021片尾曲,范范和黑人的故事,铁血南唐

本文以两种稍微有差别的方式用c#语言实现http协议的服务器类,之所以写这些,也是为了自己能更深刻了解http底层运作。

要完成高性能的web服务功能,通常都是需要写入到服务,如iis,apache tomcat,但是众所周知的web服务器配置的复杂性,如果我们只是需要一些简单的功能,安装这些组件看起来就没多大必要。我们需要的是一个简单的http类,可以很容易地嵌入到简单的web请求的服务,加到自己的程序里。

实现方法一
.net框架下有一个简单但很强大的类httplistener。这个类几行代码就能完成一个简单的服务器功能。虽然以下内容在实际运行中几乎毫无价值,但是也不失为理解http请求过程的细节原理的好途径。
复制代码 代码如下:

httplistener httplistener = new httplistener();
httplistener.authenticationschemes = authenticationschemes.anonymous;
httplistener.prefixes.add("http://localhost:8080/");
httplistener.start();
new thread(new threadstart(delegate
{
while (true)
{
httplistenercontext httplistenercontext = httplistener.getcontext();
httplistenercontext.response.statuscode = 200;
using (streamwriter writer = new streamwriter(httplistenercontext.response.outputstream))
{
writer.writeline("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/><title>测试服务器</title></head><body>");
writer.writeline("<div style=\"height:20px;color:blue;text-align:center;\"><p> hello</p></div>");
writer.writeline("<ul>");
writer.writeline("</ul>");
writer.writeline("</body></html>");
}
}
})).start();

如果你运用的好,举一反三的话,这样一个简单的类可能会收到意想不到的效果,但是由于httplistener这个类对底层的完美封装,导致对协议的控制失去灵活性,因此我想大型服务器程序里肯定不会用这个类去实现。因此如果必要的话,自己用tcp协议再去封装一个类,以便更好的控制服务运行状态。

实现方法二:
这个方法是一个老外分享的,虽然文件内容看起来很乱,其实逻辑很强很有条理。让我们来分析一下实现过程:
通过子类化httpserver和两个抽象方法handlegetrequest和handlepostrequest提供实现…
复制代码 代码如下:

public class myhttpserver : httpserver {
public myhttpserver(int port)
: base(port) {
}
public override void handlegetrequest(httpprocessor p) {
console.writeline("request: {0}", p.http_url);
p.writesuccess();
p.outputstream.writeline("<html><body><h1>test server</h1>");
p.outputstream.writeline("current time: " + datetime.now.tostring());
p.outputstream.writeline("url : {0}", p.http_url);
p.outputstream.writeline("<form method=post action=/form>");
p.outputstream.writeline("<input type=text name=foo value=foovalue>");
p.outputstream.writeline("<input type=submit name=bar value=barvalue>");
p.outputstream.writeline("</form>");
}
public override void handlepostrequest(httpprocessor p, streamreader inputdata) {
console.writeline("post request: {0}", p.http_url);
string data = inputdata.readtoend();
p.outputstream.writeline("<html><body><h1>test server</h1>");
p.outputstream.writeline("<a href=/test>return</a><p>");
p.outputstream.writeline("postbody: <pre>{0}</pre>", data);
}
}

如果你能够顺利编译和运行附件中的项目,就你应该能够用web浏览器输入http://localhost:8080/,打开就可以看上面的简单的html页面渲染。让我们看看怎么具体是怎么实现的吧~!

这个简单的web服务器可以分解为两个部分。httpserver类开启了一个指定输入端口的tcplistener实例,使用accepttcpclient()用于循环处理传入的tcp连接请求。这是处理传入的tcp连接的第一步。当传入的请求到达已知的指定端口,这个接受过程会创建一个新的服务器与客户端端口配对,用于服务器语言客户端的连接通信。
复制代码 代码如下:

view code
public abstract class httpserver {
protected int port;
tcplistener listener;
bool is_active = true;
public httpserver(int port) {
this.port = port;
}
public void listen() {
listener = new tcplistener(port);
listener.start();
while (is_active) {
tcpclient s = listener.accepttcpclient();
httpprocessor processor = new httpprocessor(s, this);
thread thread = new thread(new threadstart(processor.process));
thread.start();
thread.sleep(1);
}
}
public abstract void handlegetrequest(httpprocessor p);
public abstract void handlepostrequest(httpprocessor p, streamreader inputdata);
}

这样一些介绍方式可能会让人产生一头雾水的感觉,或许直观地看代码或调试示例源代码程序可能会更容易理解一些。下面就把源码贴上来弓大家参考,希望能对大家有所帮助!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网