http Server Example
来源:互联网 发布:淘宝店女装退货率30 编辑:程序博客网 时间:2024/04/30 13:41
/Console.Write(GetServiceOutput()); //Console.Read(); //listening port 80 int port = 80; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); TcpListener tcplistener = new TcpListener(localAddr, port); Console.WriteLine("listening on the port:{0}", port); // read incoming request tcplistener.Start(); while (true) { Socket socket = tcplistener.AcceptSocket(); byte[] buffer = new byte[4096]; socket.Receive(buffer, buffer.Length, 0); String requestStr = UTF8Encoding.UTF8.GetString(buffer).Replace("\0", ""); Console.WriteLine(requestStr); // here to parse the request // accept ?text= String context = "here is the response."; // GET if (requestStr.Contains("?text=")) { String[] operators = { "?text=", " " }; String[] tmp = requestStr.Split(operators, StringSplitOptions.RemoveEmptyEntries); context = tmp[2]; } else { if(requestStr.Contains("name=")) { String[] operators = { "name="}; String[] tmp = requestStr.Split(operators, StringSplitOptions.RemoveEmptyEntries); if(tmp.Length<=1) { context = "Please enter something."; } else { context = tmp[1]; } } else if (requestStr.Contains("num1=")) { String[] operators = { "num1=", "num2=", "&"}; String[] tmp = requestStr.Split(operators, StringSplitOptions.RemoveEmptyEntries); if (tmp.Length <= 1) { context = "Please enter something."; } else { //context = tmp[1]; // 2 parameters: num1=xxx&num2=xxx try { int num1 = int.Parse(tmp[1]); int num2 = int.Parse(tmp[2]); int result = num1 + num2; context = "the result is " + result.ToString(); } catch (Exception ex) { context = "the input is not accepted.<br/>" + ex.ToString(); } } } else { //String postContext = // "<form method='post' action='/'>" // + "<input name='uname'/>" // + "<input type='submit'/>" // + "</form>"; String postContext2 = "<form method='post' action='/'>" + "Number1:<input name='num1'/><br/>" + "Number2:<input name='num2'/><br/>" + "<input type='submit'/>" + "</form>"; context = postContext2; } } webServer ws = new webServer("AEServer"); ws.setContext(context); String reply = ws.getOKStatus(); buffer = new byte[reply.Length + 1]; int blen = UTF8Encoding.UTF8.GetBytes(reply, 0, reply.Length, buffer, 0); socket.Send(buffer, blen, 0); }
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace AWS_Console_App1{ class webServer { private String httpHeader; private String httpState; private String dateTime; private String Server; private String lastModified; private String eTag; private String contentType; private String contentLength; private String AcceptRanges; private String connection; private String context; public void setHttpHeader(String httpHeader) { this.httpHeader = httpHeader; } public void setHttpState(String httpState) { this.httpState = httpState; } public void setDateTime(String dateTime) { this.dateTime = dateTime; } public void setServer(String Server) { this.Server = Server; } public void setLastModified(String lastModified) { this.lastModified = lastModified; } public void setETag(String eTag) { this.eTag = eTag; } public void setContentType(String contentType) { this.contentType = contentType; } public void setContentLength(String contentLength) { this.contentLength = contentLength; } public void setAcceptRanges(String AcceptRanges) { this.AcceptRanges = AcceptRanges; } public void setConnection(String connection) { this.connection = connection; } public void setContext(String context) { this.context = context; } public webServer(String serverName) { this.httpHeader = "HTTP/1.1"; this.httpState = "200 OK"; this.Server = serverName; this.contentType = "text/html"; this.AcceptRanges = "bytes"; this.connection = "close"; //this.eTag = "\"56d-9989200-1132c580\""; //this.lastModified = "Wed, 18 Jun 2003 16:05:58 GMT"; } public String getOKStatus() { Console.WriteLine(this.createResponse("200 OK")); return this.createResponse("200 OK"); } public String createResponse(String httpState) { /* "HTTP/1.1 200 OK\n" "Date: Thu, 19 Feb 2009 12:27:04 GMT\n" "Server: Apache/2.2.3\n" "Last-Modified: Wed, 18 Jun 2003 16:05:58 GMT\n" "ETag: \"56d-9989200-1132c580\"\n" "Content-Type: text/html\n" "Content-Length: 15\n" "Accept-Ranges: bytes\n" "Connection: close\n" "\n" "sdfkjsdnbfkjbsf" */ StringBuilder output = new StringBuilder(); output.AppendLine("HTTP/1.1 " + httpState); output.AppendLine("Date: " + DateTime.UtcNow.ToString()); output.AppendLine("Server: " + this.Server); output.AppendLine("Last-Modified: " + this.lastModified); output.AppendLine("ETag: " + this.eTag); output.AppendLine("Content-Type: " + this.contentType); output.AppendLine("Content-Length: " + this.context.Length); output.AppendLine("Accept-Ranges: " + this.AcceptRanges); output.AppendLine("Connection: " + this.connection); output.AppendLine(); output.AppendLine(this.context); return output.ToString(); //return //"HTTP/1.1 200 OK\n" //+ "Date: Thu, 19 Feb 2009 12:27:04 GMT\n" //+ "Server: Apache/2.2.3\n" //+ "Last-Modified: Wed, 18 Jun 2003 16:05:58 GMT\n" //+ "ETag: \"56d-9989200-1132c580\"\n" //+ "Content-Type: text/html\n" //+ "Content-Length: "+ this.context.Length +"\n" //+ "Accept-Ranges: bytes\n" //+ "Connection: close\n" //+ "\n" //+ this.context; } }}