コード例 #1
0
 public void ProcessRequest(HttpContext context)
 {
     //这里并未设置contextType,而是使用了默认的text格式
     context.Response.ResponseBody =
         Encoding.UTF8.GetBytes(string.Format("<html><body><h3>{0}</h3></body></ html>",DateTime.Now.ToString()));
     context.Response.ResponseStatus = 200;
 }
コード例 #2
0
        /// <summary>
        /// 处理http上下文
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            //获得文件扩展名
            string ex = Path.GetExtension(context.Request.Url);
            //获得exe文件的位置
            string binPath = AppDomain.CurrentDomain.BaseDirectory;
            //http报文url范例:/FirstWeb/FirstShower.ashx
            string filePath=Path.Combine(binPath,context.Request.Url.TrimStart(new char[]{'/'}));

            //处理动态页面
            if (ex==".aspx")
            {
                string className=Path.GetFileNameWithoutExtension(filePath);
                //加载当前项目程序集,因为整个项目的代码都在exe文件中,从该exe中反射出类,并实例化,调用接口方法
                IHttpProcess demo =
                    Assembly.Load("SocketProgram").CreateInstance("SocketProgram.IISClass." + className) as IHttpProcess;
                demo.ProcessRequest(context);
                return;
            }

            //处理静态页面
            //如果文件存在
            if (File.Exists(filePath))
            {
                byte[] bodyByte=File.ReadAllBytes(filePath);
                context.Response.ResponseBody = bodyByte;
                context.Response.ResponseStatus = 200;
            }
            else//如果没有找到文件返回错误页面
            {
                //把要访问的页面改成错误页面的Url,保证获取到正确的ContextType
                context.Request.Url = "/WebSite/404.htm";
                string errorPagePath = Path.Combine(binPath, "WebSite\\404.htm");
                byte[] errorBodyByte = File.ReadAllBytes(errorPagePath);
                context.Response.ResponseBody = errorBodyByte;
                context.Response.ResponseStatus = 404;
            }
        }
コード例 #3
0
        /// <summary>
        /// 等待浏览器请求,连接一次就断开
        /// </summary>
        /// <param name="sokect">监听socket</param>
        public void WaitForRequest(object sokect)
        {
            Socket listenSocket = sokect as Socket;
            byte[] receiveData=new byte[2*1024*1024];
            while (true)
            {
                //这里一直监听请求,但是浏览器可能会发送0字节请求,以表示断开连接
                //但下面代码已经自动断开连接,所以0字节数据会当做普通请求处理,所以加个判断
                Socket connectSocket=listenSocket.Accept();
                int length=connectSocket.Receive(receiveData, receiveData.Length, SocketFlags.None);
                if (length<=0)
                {
                    continue;
                }

                //获取请求报文
                string respondStr = Encoding.UTF8.GetString(receiveData, 0, length);
                HttpContext context = new HttpContext(respondStr);

                //把报文和连接信息显示在文本框
                AppendTextToMsg(String.Format("{0}:连接成功", connectSocket.RemoteEndPoint.ToString()));
                AppendTextToMsg(string.Format("接收客户端{0}的消息:{1}", connectSocket.RemoteEndPoint.ToString(), respondStr));

                //处理报文
                ProcessDemo1 process = new ProcessDemo1();
                process.ProcessRequest(context);

                //返回响应报文
                connectSocket.Send(context.Response.GetResponseHead());
                connectSocket.Send(context.Response.ResponseBody);

                //关闭sockect
                connectSocket.Shutdown(SocketShutdown.Both);
                connectSocket.Close();

            }
        }