如何向tcp服务器发送http请求?我想创建一个侦听端口443的TCP服务器,以便它可以接收HTTP请求并回发。现在我正在使用Apache和PHP以正常方式执行此操作,但是否可以在没有Web服务器的情况下执行此操作?例如,我用C#.NET构建了一个传统的TCP/IP客户端/服务器应用程序。是否可以使用此实现来处理HTTP请求而不是使用Web服务器?这个项目可以帮助您从头开始创建Web服务器:MiniHttpd:一个HTTPWeb服务器库,它使用纯TCP/IP,但会处理许多不必要的细节,例如解析标头、压缩、身份验证、ssl等。我会使用内置的HttpListener类。这是一个简化的(但有效的)多线程WebServervoidCreateWebServer(){HttpListenerlistener=newHttpListener();listener.Prefixes.Add("http://*:8080/");监听器.Start();newThread(()=>{while(true){HttpListenerContextctx=listener.GetContext();ThreadPool.QueueUserWorkItem((_)=>ProcessRequest(ctx));}}).Start();}voidProcessRequest(HttpListenerContextctx){stringresponseText="Hello";byte[]buf=Encoding.UTF8.GetBytes(responseText);Console.WriteLine(ctx.Request.Url);ctx.Response.ContentEncoding=Encoding.UTF8;ctx.Response.ContentType="文本/html";ctx.Response.ContentLength64=buf.Length;ctx.Response.OutputStream.Write(buf,0,buf.Length);ctx.Response.Close();编辑我稍微改变了服务器使其成为RESTfulWebService(MyService)的主机。它以Json的形式返回结果。您可以将其称为http://localhost:8080/?method=Calc&i=5&j=4(为简单起见,我省了很多错误检查)publicclassMyService{publicResultCalc(inti,intj){returnnewResult(){加法=i+j,乘法=i*j};}}publicclassResult{publicintAddition{set;得到;}publicint乘法{设置;得到;}}voidCreateWebServer(){MyServiceservice=newMyService();HttpListener监听器=newHttpListener();listener.Prefixes.Add("http://*:8080/");监听器.Start();newThread(()=>{while(true){HttpListenerContextctx=listener.GetContext();ThreadPool.QueueUserWorkItem((_)=>ProcessRequest(ctx,service));}}).Start();}voidProcessRequest(HttpListenerContextctx,MyServiceservice){try{stringresponseText=Execute(ctx,service);byte[]buf=Encoding.UTF8.GetBytes(responseText);ctx.Response.ContentEncoding=Encoding.UTF8;ctx.Response.ContentType="应用程序/json";ctx.Response.ContentLength64=buf.Length;ctx.响应.OutputStream.Write(buf,0,buf.Length);}catch{ctx.Response.StatusCode=(int)HttpStatusCode.NotFound;}ctx.Response.Close();}stringExecute(HttpListenerContextctx,MyServiceservice){System.Collections.Specialized.NameValueCollectionnv=HttpUtility.ParseQueryString(ctx.Request.Url.Query);MethodInfomi=service.GetType().GetMethod(nv["方法"]);object[]parameters=mi.GetParameters().Select(pi=>Convert.ChangeType(nv[pi.Name],pi.ParameterType)).ToArray();返回JsonConvert.SerializeObject(mi.Invoke(service,parameters));在最低级别,您可以使用HTTP侦听器类执行此操作http://geekswithblogs.net/Nitin/archive/2008/06/22/using-the-http-listener-class.aspx我曾经写过类似的东西uni项目,所以如果你有任何具体问题,我可以查找代码。如果你想更上一层楼,你可以考虑网络服务。这是我的代码的一部分......看看你能用它做什么。:IP地址address=IPAddress.Parse("127.0.0.1");IPEndPointport=newIPEndPoint(地址,9999);//端口9999TcpListenerlistener=newTcpListener(port);监听器.Start();Console.WriteLine("--服务器加载--");while(true)//永远循环{Console.WriteLine("WaitingforNewClient");套接字sock=listener.AcceptSocket();字节[]缓冲区=新字节[32];字符串incomingMessage="";//读取:while(sock.Available>0){intgotBytes=sock.Receive(buffer);incomingMessage+=Encoding.ASCII.GetString(buffer,0,gotBytes);}//调试://Console.WriteLine(incomingMessage);//现在检查它是GET还是POSTif(incomingMessage.ToUpper().Contains("POST")&&incomingMessage.ToUpper().Contains("/DOSEARCH"))//已请求搜索.WriteLine("查询已收到");//提取帖子数据字符串htmlPostData=incomingMessage.Substring(incomingMessage.IndexOf("songName"));string[]参数=htmlPostData.Split('&');字符串[]输入=新字符串[5];for(inti=0;i...以上就是C#学习教程:如何向tcp服务器发送http请求?全部内容分享给大家,如果对C#学习教程有帮助,需要了解更多,我希望大家多多关注---byte[]outbuffer=Encoding.ASCII.GetBytes(answerPage.ToString());sock.Send(outbuffer);Console.WriteLine("ResultsSent");sock.Close();这个文章收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除,如需转载请注明出处:
