`

ASP.NET Request Response Server

 
阅读更多

Request对象

Page类的一个属性.

public HttpResponse Response{get;}

 

 

Request对象常用属性

1. Request.ApplicationPath

    获得服务器的虚拟目录路径,例如页面路径为:http://localhost:29723/WebSite2/Default.aspx,获得的结果为/WebSite2。

 

2. Request.AppRelativeCurrentExecutionFilePath

    获得当前页面相对根目录的路径,例如:~/Default.aspx。

 

3. Request.Browser.ClrVersion

    获得服务器的.NET Framework版本.

 

4. Request.ContentEncoding

    获得页面编码方式的对象表示。

 

5. Request.CurrentExecutionFilePath

    获得当前请求的虚拟路径,例如:/WebSite2/Default.aspx

 

6. Request.FilePath

    获得当前文件的虚拟路径,例如:/WebSite2/Default.aspx。跳转方式不一样时该参数与上一个参数可能存在差异。

 

7. Request.HttpMethod

    获得客户端请求服务器页面的方式。GET/POST等等。

 

8. Request.Path

    获得当前页面的相对虚拟路径。

 

9. Request.PathInfo

    获得虚拟目录的服务器磁盘绝对路径。

 

10. Request.PhysicalApplicationPath

    获得当前应用的服务器磁盘绝对路径。

 

11. Request.PhysicalPath

    获得页面的服务器磁盘绝对路径。

 

Request.QueryString

    获取请求的参数字符串。

 

12. Request.RequestType

    获得客户端请求服务器页面的方式。GET/POST等等。

 

13. Request.Url

    获得客户端请求的URL。

 

14. Request.RowUrl

    获得原始请求URL。

 

14. Request.UserHostAddress

    获取请求客户端的IP地址。

 

15. Request.UserHostName

    获取请求客户端的用户名。

 

16. Request.UserLanguage

    获得请求客户端浏览器支持的语言。

 

17. Request.UrlReferrer

    网页的来源。可以根据该属性判断从百度搜的关键字、防下载盗链、防图片盗链。

    注意:该属性可以伪造(例如迅雷)。

 

Request对象常用方法:

Request.MapPath(string virtulPath)

    将虚拟路径转换为磁盘上的物理路径。

    Request.MapPath("~/a/b.aspx")得到D:\2008\WebSites\a\b.aspx 。

 

 

例:在一般处理程序中输出图片,并且防止盗链。并作IP屏蔽。如果正常输出,则先添加IP显示的水印。

 

using System;
using System.Web;

public class Pic1:IHttpHandler{
    public void ProcessRequest(HttpContext context){
        context.Response.ContentType="image/JPEG";
        string fullpath=HttpContext.Current.Server.MapPath("a.jpg");
        using(System.Drawing.Bitmap bitmap=new System.Drawing.Bitmap(fullpath))
        {
            using(System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(bitmap))
            {
                if(context.Request.UrlReferrer==null)
                {
                    g.Clear(System.Drawing.Color.White);
                    g.DrawString("禁止直接浏览图片",new System.Drawing.Font("宋体",30),System.Drawing.Brushes.Red,0,0);
                }
                else if(context.Request.UrlReferrer.Host!="localhost")
                {
                    g.Clear(System.Drawing.Color.White);
                    g.DrawString("图片只限内部使用",new System.Drawing.Font("宋体",30),System.Drawing.Brushes.Red,0,0);
                }
                g.DrawString("你的IP:"+context.Request.UserHostAddress,new System.Drawing.Font("宋体",30),System.Drawing.Brushes.Red,0,0);
                if(context.Request.UserHostAddress=="127.0.0.1" || context.Request.UserHostAddress=="192.168.10.12")
                {
                    g.Clear(System.Drawing.Color.Blue);
                    g.DrawString("你的IP被屏蔽。",new System.Drawing.Font("宋体",30),System.Drawing.Brushes.Red,0,0);
                }
            }
            bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
        }

    }

}
 

 

 

Response对象

Page类的一个属性。

 

响应的缓冲输出:为提高服务器性能,ASP.NET想浏览器Write的时候默认不会每次都立即输出到浏览器,而是会缓存数据,到合适的时候或者响应结束才会将缓存中的数据一起发送到浏览器。

 

Response的主要成员

1. Response.Buffer。Response.BufferOutput:这两个属性是一样的Buffer内部调用了BufferOutput。该属性用于控制是否采用响应缓冲,默认是true。

 

2. Response.Flush()将缓存区中的数据发送给浏览器。在需要将Write出的内容立即输出到浏览器的场合非常实适用。

 

3. Response.Clear()清空缓存中的数据。不发送到浏览器。可以在缓存输出之前调用该方法。

 

4. Response.ContentEncoding输出流的编码。一般不用考虑。但可以通过该属性来读取和设置。

 

5. Response.ContentType输出流的内容类型,比如是html(text/html)还是普通文本(text/plain),还是JPEG图片(image/JPEG)。

 

6. Response.Cookies返回浏览器的Cookie的集合,可以通过它设置Cookie。一般情况下不需要处理Cookies。

 

7. Response.OutputStream 输出流。

 

8. Response.End()终止响应。使用该方法会终止执行调用的过程。

 

9. Response.Redrect(urlpath)重定向浏览器。可以重定向到外部网站。在浏览器地址栏看到url发送改变。

 

10. Response.SetCookie(HttpCookie cookie)向输出流更新写到浏览器中的Cookie,如果Cookie存在就更新,不存在就增加,是对Response.Cookies的简化调用。

 

10.Response.Write()向浏览器输出内容。Write方法输出的内容在渲染输出的页面之前。

 

11.Response.WriteFile(string filename) 将文件输出给浏览器。

 

注意:

1.不建议在aspx中输出非html的内容。

2.二进制内容使用OutputStream。如图片,下载文件。文本数据使用Write。

 

 

 

 

Server对象

context的一个属性,是HttpServerUtility类的一个对象。

 

1.编码

Server.HtmlDecode(string)、Server.HtmlEncode(string)、

Server.UrlDecode(string)、Server.UrlEncode(string)

进行html解码与编码。主要防治XSS攻击。

HtmlDecode,HtmlEncode,UrlDecode,UrlEncode方法是对HttpUtility类中相应方法的代理调用。推荐使用HttpUtility类,因为有些地方不太容易获取到Server对象。

UrlDecode,UrlEncode一般可以处理非Ascii码的url。

 

2. Server.Transfer(path)内部重定向。浏览器地址不发生改变。由于是内部请求,所以Request、Cookies等这些来源页面接受的参数可以在重定向页面中获得。

注意:不能重定向到ashx,否则会报错"执行子请求出错"。

如果需要这样访问ashx,需要做额外处理。

 

3. 使用优势不能得到HttpContext对象,比如在Global.asax中,可以通过HttpContext.Current取得当前的HttpContext对象。进而取得Response,Request,Server等。

 

4. Server.MapPath 同request.MapPath。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics