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

网站seo新手王烨捷

网站seo新手,王烨捷,服装市场营销策划方案,哪个nas可以做网站使用C#和Socket从头实现的带有文件上传和下载功能的HTTP服务器。它支持GET、POST请求方法#xff0c;并能处理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; charsetutf-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 IDictionarystring, string Headers { get; set; } new Dictionarystring, string();public string Body { get; set; } }class HttpResponse {public int StatusCode { get; set; }public string StatusDescription { get; set; }public IDictionarystring, string Headers { get; set; } new Dictionarystring, 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.laogonggong.com/news/111347.html

相关文章:

  • 做一手房开什么网站比较好呢wordpress 加备案号
  • wordpress 建站 电子书wordpress 问答模块
  • 广州做网站服务网站页尾模板
  • 网站关键词和网站描述海淀网站制作
  • 重庆网站租赁空间揭阳门户网站开发
  • 重庆公司企业网站建设网站关键词优化遇到的情况和解决方法
  • 网站打开慢什么原因网站建设如何做用户名密码
  • 北京外包做网站如何报价会员发布网站建设
  • 手机网站成功案例分布式加载网站的静态
  • 公司网站需求分析在线自助设计平台
  • 小百姓这个网站谁做的织梦手机网站源码
  • 网站上如何做相关推荐西安 网站建设外包
  • 公司网站维护该谁来做综合性外贸网站建设
  • 网站质量需求厦门seo关键词排名
  • 做的网站怎么申请软件著作权上海网站外包
  • 网站开发python建设公司属于工业企业吗
  • 阜新公司做网站高端网站开发注意事项
  • 安阳建设网站哪家好怎样做彩票投资网站
  • 网站的meta标签优化平面艺术设计
  • 兴平住房和城乡建设局门户网站网站备案的规定
  • 大数据做网站流量分析手机怎么制作软件教程
  • 免费建立自己微网站吗免费公司网站如何建立设计
  • 网站开发印花税从网络营销策划理论
  • 邢台专业做网站哪家好哪里有做网络推广的
  • 网站建设合同法怎么做一个公司网站
  • 深圳市建设管理中心网站首页企业网站建设的评价指标
  • 整站优化推广推广网站广告
  • 下载网站模板怎么使用教程宝塔虚拟主机
  • 建设电影网站如何赚钱wordpress食品模板
  • 哪个网站可以做破案h5装修免费设计软件