一聚教程网:一个值得你收藏的教程网站

热门教程

一个用C#实现的简单http server(转自中华技术网 www.driverdevelop.co

时间:2022-07-02 12:03:27 编辑:袖梨 来源:一聚教程网

http.cs
----------------------------
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class HttpProcessor {
private Socket s;
private BufferedStream bs;
private StreamReader sr;
private StreamWriter sw;
private String method;
private String url;
private String protocol;
private Hashtable hashTable;
public HttpProcessor(Socket s) {
this.s = s;
hashTable = new Hashtable();
}
public void process() {
NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite);
bs = new BufferedStream(ns);
sr = new StreamReader(bs);
sw = new StreamWriter(bs);
parseRequest();
readHeaders();
writeURL();
s.Shutdown(SocketShutdown.SdBoth);
ns.Close();
}
public void parseRequest() {
String request = sr.ReadLine();
string[] tokens = request.Split(new char[]{' '});
method = tokens[0];
url = tokens[1];
protocol = tokens[2];
}
public void readHeaders() {
String line;
while((line = sr.ReadLine()) != null && line != "") {
string[] tokens = line.Split(new char[]{':'});
String name = tokens[0];
String value = "";
for(int i = 1; i < tokens.Length; i++) {
value += tokens[i];
if(i < tokens.Length - 1) tokens[i] += ":";
}
hashTable[name] = value;
}
}
public void writeURL() {
try {

热门栏目