当前位置: 首页 > news >正文

清欢互联网网站建设武汉seo服务多少钱

清欢互联网网站建设,武汉seo服务多少钱,网站建设方案打包,济南高端网站设计策划使用C#和Socket从头实现的带有文件上传和下载功能的HTTP服务器。它支持GET、POST请求方法,并能处理URL参数、请求体以及文件上传和下载。 using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text;class HttpServer {publi…

使用C#和Socket从头实现的带有文件上传和下载功能的HTTP服务器。它支持GET、POST请求方法,并能处理URL参数、请求体以及文件上传和下载。

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;class HttpServer
{public static void Main(string[] args){const int port = 8080;TcpListener listener = new TcpListener(IPAddress.Any, port);listener.Start();Console.WriteLine("HTTP server is running on port {0}...", port);while (true){TcpClient client = listener.AcceptTcpClient();ProcessClientRequest(client);}}public static void ProcessClientRequest(TcpClient client){using (NetworkStream stream = client.GetStream()){// 读取请求数据byte[] buffer = new byte[4096];int bytesRead = stream.Read(buffer, 0, buffer.Length);string requestString = Encoding.UTF8.GetString(buffer, 0, bytesRead);// 解析请求HttpRequest request = ParseRequest(requestString);// 构造响应数据HttpResponse response = BuildResponse(request);// 发送响应头byte[] responseHeaderBytes = Encoding.UTF8.GetBytes(response.GetHeaderString());stream.Write(responseHeaderBytes, 0, responseHeaderBytes.Length);// 发送响应体(如果有)if (response.ContentStream != null){byte[] bufferBytes = new byte[4096];int bytesToRead;while ((bytesToRead = response.ContentStream.Read(bufferBytes, 0, bufferBytes.Length)) > 0){stream.Write(bufferBytes, 0, bytesToRead);}response.ContentStream.Close();}}client.Close();}public static HttpRequest ParseRequest(string requestString){var request = new HttpRequest();string[] lines = requestString.Split(new[] { "\r\n" }, StringSplitOptions.None);// 解析请求行string[] requestLineParts = lines[0].Split(' ');request.Method = requestLineParts[0].ToUpper();request.Path = requestLineParts[1];// 解析请求头for (int i = 1; i < lines.Length; i++){string[] headerParts = lines[i].Split(':');if (headerParts.Length == 2){string key = headerParts[0].Trim();string value = headerParts[1].Trim();request.Headers[key] = value;}}// 解析请求体(仅对POST请求处理)if (request.Method == "POST"){int bodyIndex = Array.IndexOf(lines, "");if (bodyIndex != -1 && bodyIndex < lines.Length - 1){request.Body = lines[bodyIndex + 1];}}return request;}public static HttpResponse BuildResponse(HttpRequest request){var response = new HttpResponse();// 设置响应头信息response.StatusCode = 200;response.StatusDescription = "OK";response.Headers["Content-Type"] = "text/plain; charset=utf-8";// 处理文件上传if (request.Method == "POST" && request.Headers.ContainsKey("Content-Disposition")){string filename = GetFilenameFromContentDisposition(request.Headers["Content-Disposition"]);using (FileStream fileStream = File.Create(filename)){using (StreamWriter writer = new StreamWriter(fileStream)){writer.Write(request.Body);}}response.SetContent("File uploaded successfully.");}// 处理文件下载else if (request.Method == "GET" && request.Path.StartsWith("/download/")){string filepath = request.Path.Substring("/download/".Length);if (File.Exists(filepath)){response.StatusCode = 200;response.StatusDescription = "OK";response.Headers["Content-Type"] = "application/octet-stream";response.Headers["Content-Disposition"] = $"attachment; filename=\"{Path.GetFileName(filepath)}\"";response.ContentStream = File.OpenRead(filepath);}else{response.StatusCode = 404;response.StatusDescription = "Not Found";response.SetContent("File not found.");}}// 默认返回文本内容else{string content = "Welcome to the HTTP server.";response.SetContent(content);}return response;}public static string GetFilenameFromContentDisposition(string contentDisposition){const string keyword = "filename=\"";int startIndex = contentDisposition.IndexOf(keyword) + keyword.Length;int endIndex = contentDisposition.IndexOf("\"", startIndex);return contentDisposition.Substring(startIndex, endIndex - startIndex);}
}class HttpRequest
{public string Method { get; set; }public string Path { get; set; }public IDictionary<string, string> Headers { get; set; } = new Dictionary<string, string>();public string Body { get; set; }
}class HttpResponse
{public int StatusCode { get; set; }public string StatusDescription { get; set; }public IDictionary<string, string> Headers { get; set; } = new Dictionary<string, string>();public Stream ContentStream { get; set; }public void SetContent(string content){byte[] contentBytes = Encoding.UTF8.GetBytes(content);ContentStream = new MemoryStream(contentBytes);Headers["Content-Length"] = contentBytes.Length.ToString();}public string GetHeaderString(){StringBuilder builder = new StringBuilder();builder.AppendFormat("HTTP/1.1 {0} {1}\r\n", StatusCode, StatusDescription);foreach (var header in Headers){builder.AppendFormat("{0}: {1}\r\n", header.Key, header.Value);}builder.Append("\r\n");return builder.ToString();}
}

http://www.hrbkazy.com/news/25416.html

相关文章:

  • 做网站图片怎么做站长之家seo概况查询
  • 技术支持 哈尔滨网站建设百度下载安装到手机
  • 怎样给自己的网站做优化互联网广告管理暂行办法
  • 网站的思维导图怎么做黄页网络的推广网站有哪些软件
  • 南京广告公司装修企业seo关键词优化
  • 河南高端网站建设韶关疫情最新消息
  • 上海网站建站最新的疫情数据
  • 个人网站论文结束语平台seo
  • 网站做程序员跨界营销案例
  • 如何创建div做网站网络营销网络推广
  • 用自己电脑做服务器 网站吗seo服务商技术好的公司
  • 网站空间怎么购买互联网营销师培训课程
  • 网站建站哪个公司好一点百度百度一下官网
  • 做资源网站盈利点推广联盟
  • 做介绍英文网站软文素材库
  • 网站模板 缓存商标网站优化方式有哪些
  • 怎么制作网站来赚钱下载百度网盘app
  • 西宁做网站公司哪家好企业宣传推广方案
  • 做网站gif代码朋友圈广告代理商官网
  • 51网站空间相册在哪里广州新闻头条最新消息
  • b2b商贸网站系统网络营销发展现状与趋势
  • h5自适应网站模板下载网站制作流程是什么
  • 涉密资质 网站建设韶山seo快速排名
  • 郑州建设厅网站天津百度优化
  • 管理公司网站建设广东深圳疫情最新消息
  • 昆明响应式网站制作2021百度最新收录方法
  • 网站上的高清图怎么做门户网站推广方案
  • 商城网站建设策划佛山seo教程
  • 做旅行网站的依据及意义网络营销活动案例
  • 做公司网站需要菏泽百度推广公司电话