# HttpServer
一个使用C#编写的简易Web服务器, 目前支持:
* 静态页面处理
* GET/POST请求
* 支持HTTPS协议
* 支持返回JSON(**Todo**)
* 支持路由方法(**Todo**)
# 快速开始
## HTTP服务器示例
```
class Program
{
static void Main(string[] args)
{
ExampleServer server = new ExampleServer("0.0.0.0",4050);
server.Start();
}
}
```
## GET/POST请求示例
```
public override void OnPost(HttpRequest request, HttpResponse response)
{
//获取客户端传递的参数
string data = request.Params == null ? "" : string.Join(";", request.Params.Select(x => x.Key + "=" + x.Value).ToArray());
//设置返回信息
string content = string.Format("这是通过Post方式返回的数据:{0}", data);
//构造响应报文
response.SetContent(content);
response.Content_Encoding = "utf-8";
response.StatusCode = "200";
response.Content_Type = "text/html; charset=UTF-8";
response.Server = "ExampleServer";
//发送响应
response.Send();
}
public override void OnGet(HttpRequest request, HttpResponse response)
{
///链接形式1:"http://localhost:4050/assets/styles/style.css"表示访问指定文件资源,
///此时读取服务器目录下的/assets/styles/style.css文件。
///链接形式1:"http://localhost:4050/assets/styles/"表示访问指定页面资源,
///此时读取服务器目录下的/assets/styles/style.index文件。
//当文件不存在时应返回404状态码
string requestURL = request.URL;
requestURL = requestURL.Replace("/", @"\").Replace("\\..", "").TrimStart('\\');
string requestFile = Path.Combine(ServerRoot, requestURL);
//判断地址中是否存在扩展名
string extension = Path.GetExtension(requestFile);
//根据有无扩展名按照两种不同链接进行处
if (extension != "")
{
//从文件中返回HTTP响应
response = LoadFromFile(response, requestFile);
}
else
{
//目录存在且不存在index页面时时列举目录
if (Directory.Exists(requestFile) && !File.Exists(requestFile + "\\index.html"))
{
requestFile = Path.Combine(ServerRoot, requestFile);
var content = ListDirectory(requestFile, requestURL);
response = response.SetContent(content, Encoding.UTF8);
response.Content_Type = "text/html; charset=UTF-8";
}
else
{
//加载静态HTML页面
requestFile = Path.Combine(requestFile, "index.html");
response = LoadFromFile(response, requestFile);
response.Content_Type = "text/html; charset=UTF-8";
}
}
//发送HTTP响应
response.Send();
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
HttpServer C#
共92个文件
cs:24个
xml:10个
config:9个
3星 · 超过75%的资源 需积分: 20 82 下载量 18 浏览量
2018-08-17
09:04:42
上传
评论
收藏 270KB RAR 举报
温馨提示
HttpServer C# 通过winform启动http服务器 HttpServer C# 通过winform启动http服务器
资源推荐
资源详情
资源评论
收起资源包目录
HttpServer-master.rar (92个子文件)
HttpServer-master
.gitignore 3KB
README.md 3KB
HTTPServer
HttpServerLib
ServiceRoute.cs 585B
RouteMethod.cs 214B
HttpServerLib.sln 915B
HttpServerLib.csproj 3KB
packages.config 134B
HttpResponse.cs 8KB
obj
Debug
TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs 0B
HttpServerLib.pdb 72KB
HttpServerLib.csproj.FileListAbsolute.txt 916B
DesignTimeResolveAssemblyReferencesInput.cache 6KB
HttpServerLib.dll 29KB
TempPE
HttpServerLib.csprojResolveAssemblyReference.cache 2KB
TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs 0B
TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs 0B
x86
Debug
HttpServerLib.csproj.FileListAbsolute.txt 116B
DesignTimeResolveAssemblyReferencesInput.cache 6KB
TempPE
bin
Release
Debug
HttpServerLib.pdb 72KB
HttpServerLib.dll 29KB
HttpServerLib.suo 17KB
HttpServer.cs 12KB
HttpService.cs 2KB
Protocols.cs 223B
ActionResult.cs 319B
HttpRequest.cs 12KB
ILogger.cs 223B
ServiceModule.cs 1KB
HttpServerLib.csproj.user 228B
RouteAttribute.cs 335B
Properties
AssemblyInfo.cs 1KB
IServer.cs 772B
BaseHeader.cs 7KB
.vs
HTTPServer
v14
.suo 132KB
HTTPServer.suo 9KB
HTTPServer.v11.suo 100KB
Packages
repositories.config 183B
LitJson.0.7.0
LitJson.0.7.0.nupkg 23KB
lib
LitJson.dll 52KB
HTTPServer
Program.cs 544B
HttpServer.csproj.user 227B
HttpServer_.csproj 4KB
packages.config 134B
obj
Debug
TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs 0B
HttpServer.pdb 22KB
HttpServer.csproj.FileListAbsolute.txt 1KB
HttpServer.csprojResolveAssemblyReference.cache 6KB
DesignTimeResolveAssemblyReferencesInput.cache 6KB
TempPE
TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs 0B
TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs 0B
HttpServer.exe 59KB
x86
Debug
HttpServer.csproj.FileListAbsolute.txt 98B
DesignTimeResolveAssemblyReferencesInput.cache 6KB
TempPE
bin
Release
Debug
HttpServer.vshost.exe.config 184B
HttpServer.pdb 22KB
HttpServerLib.pdb 72KB
WindowsFormsHttpsPost.exe.config 182B
HttpServer.vshost.exe.manifest 490B
vtmc.bocgroup.com.p12 3KB
WindowsFormsHttpsPost.vshost.exe 11KB
SM2-CA.crt 2KB
EaiOut
PBS_28017______I0000001_HKG.xml 2KB
CIN_CIDLINKC25_I0000001_HKG.xml 19KB
PBS_PBDINQ_08__I0000001_HKG.xml 3KB
PBS_28017______I0000001_HKG(INVALID CUSTOMER CLASS).xml 3KB
PBS_PBDINQ_08__I0000001_HKG(level4).xml 3KB
MDP_00003______I0000001_HKG.xml 2KB
MDP_00001______I0000001_HKG.xml 3KB
PBS_PBDINQ_08__I0000001_HKG(level1).xml 3KB
PBS_28017______I0000001_HKG(Invalid ID type).xml 3KB
HttpServerLib.dll 29KB
WindowsFormsHttpsPost.vshost.exe.config 182B
EaiOffline.rar 15KB
HttpServer.exe.config 184B
HttpServer.exe 59KB
HTTPServer.vshost.exe 11KB
EaiOffline
EaiOut
PBS_28017______I0000001_HKG.xml 2KB
HttpServerLib.dll 26KB
HttpServer.exe.config 184B
HttpServer.exe 9KB
WindowsFormsHttpsPost.vshost.exe.manifest 490B
HttpServer.sln 909B
HttpServer.csproj 3KB
ConsoleLogger.cs 317B
ExampleServer.cs 2.9MB
App.config 184B
HttpServer.suo 16KB
Properties
AssemblyInfo.cs 1KB
HTTPServer_10.sln 1KB
HTTPServer.sln 1KB
HTTPServer_10.suo 37KB
Packages.dgml 474B
共 92 条
- 1
资源评论
- dimple_liu2019-09-03很全面的server。很好。
- cto_hanlaoshi2020-02-02这个应该是抄袭,并且不能用。
gzlykao
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功