最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
一个用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 {
----------------------------
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 {
相关文章
- 《原神》5.2卡池抽取建议 11-14
- 《原神》5.2版本新怪物介绍 11-14
- 《原神》希诺宁增伤触发方法 11-14
- 《原神》循音觅奇活动入口 11-14
- 《原神》循音觅奇兑换码获取方法 11-14
- 《原神》花羽会活动飞行技巧介绍 11-14