HttpResponse 类

而封闭HTTP输出信息的类型就是HttpResponse类,使用HttpResponse类可以实现三种类型的输出,即文本,URL,二进制流.
  实现这三类的属性和方法分别介绍如下:
1.文本的输出,在日常开发中,后台中的文本可能需要输出到浏览器中,让用户浏览,这就需要实现动态HTML的输出,使用HttpResponse类的Write静态方法可以实现,例如希望在浏览器上显示一个"hello world!"的字样时,可以在Page_load方法中增加如下代码,就可以实现:
 

 Response.write("hello world!")


2.URL的输出,程序开发经常需要根据情况将用户浏览的界面重定向到其他页面,例如,用户在没有登录的状态下查看自己的信息,系统需要首先将其转向到登录页,登录后再转回信息浏览页,实现URL的输出可以使用HttpResponse类的redirect方法实现,代码如下:
  

response.redirect("http://www.djjwz.com/")


3.二进制流,有时需要将服务器上的文件提供给用户下载,或者在浏览器端动态生成一幅图片,例如,验证的初一二进制流输出到用户浏览器中.

https://msdn.microsoft.com/zh-cn/library/system.web.httpresponse(v=vs.110).aspx

封装来自 ASP.NET 操作的 HTTP 响应信息

已用到的方法:

System_CAPS_pubmethod Redirect(String)

将请求重定向到新 URL 并指定该新 URL。

System_CAPS_pubmethod Redirect(String, Boolean)

将客户端重定向到新的 URL。指定新的 URL 并指定当前页的执行是否应终止。

System_CAPS_pubmethod RedirectPermanent(String)

执行从所请求 URL 到所指定 URL 的永久重定向。

System_CAPS_pubmethod RedirectPermanent(String, Boolean)

执行从所请求 URL 到所指定 URL 的永久重定向,并提供用于完成响应的选项。

System_CAPS_pubmethod RedirectToRoute(Object)

使用路由参数值将请求重定向到新 URL。

System_CAPS_pubmethod RedirectToRoute(RouteValueDictionary)

使用路由参数值将请求重定向到新 URL。

System_CAPS_pubmethod RedirectToRoute(String)

使用路由名称将请求重定向到新 URL。

System_CAPS_pubmethod RedirectToRoute(String, Object)

使用路由参数值和路由名称将请求重定向到新 URL。

System_CAPS_pubmethod RedirectToRoute(String, RouteValueDictionary)

使用路由参数值和路由名称将请求重定向到新 URL。

System_CAPS_pubmethod RedirectToRoutePermanent(Object)

使用路由参数值执行从所请求 URL 到新 URL 的永久重定向。

System_CAPS_pubmethod RedirectToRoutePermanent(RouteValueDictionary)

使用路由参数值执行从所请求 URL 到新 URL 的永久重定向。

System_CAPS_pubmethod RedirectToRoutePermanent(String)

使用路由名称执行从所请求 URL 到新 URL 的永久重定向。

System_CAPS_pubmethod RedirectToRoutePermanent(String, Object)

使用路由参数值以及与新 URL 对应的路由的名称执行从所请求 URL 到新 URL 的永久重定向。

System_CAPS_pubmethod RedirectToRoutePermanent(String, RouteValueDictionary)

使用路由参数值和路由名称执行从所请求 URL 到新 URL 的永久重定向。

System_CAPS_pubmethod ToString()

返回表示当前对象的字符串。(从 Object 继承。)

System_CAPS_pubmethod TransmitFile(String)

将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。

System_CAPS_pubmethod TransmitFile(String, Int64, Int64)

将文件的指定部分直接写入 HTTP 响应输出流,而不在内存中缓冲它。

System_CAPS_pubmethod Write(Char)

将字符写入 HTTP 响应输出流。

System_CAPS_pubmethod Write(Char[], Int32, Int32)

将字符数组写入 HTTP 响应输出流。

System_CAPS_pubmethod Write(Object)

将 Object 写入 HTTP 响应流。

System_CAPS_pubmethod Write(String)

将字符串写入 HTTP 响应输出流。

System_CAPS_pubmethod WriteFile(IntPtr, Int64, Int64)

将指定的文件直接写入 HTTP 响应输出流。

System_CAPS_pubmethod WriteFile(String)

将指定文件的内容作为文件块直接写入 HTTP 响应输出流。

System_CAPS_pubmethod WriteFile(String, Boolean)

将指定文件的内容作为内存块直接写入 HTTP 响应输出流。

System_CAPS_pubmethod WriteFile(String, Int64, Int64)

将指定的文件直接写入 HTTP 响应输出流。

System_CAPS_pubmethod WriteSubstitution(HttpResponseSubstitutionCallback)

允许将响应替换块插入响应,从而允许为缓存的输出响应动态生成指定的响应区域。

代码示例:

<%@ Page Language="C#" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    private void Page_Load(object sender, EventArgs e)
    {
// <snippet2>
        // Set the page's content type to JPEG files
        // and clears all content output from the buffer stream.
        Response.ContentType = "image/jpeg";
        Response.Clear();

        // Buffer response so that page is sent
        // after processing is complete.
        Response.BufferOutput = true;
// </snippet2>

        // Create a font style.
        Font rectangleFont = new Font(
            "Arial", 10, FontStyle.Bold);

        // Create integer variables.
        int height = 100;
        int width = 200;

        // Create a random number generator and create
        // variable values based on it.
        Random r = new Random();
        int x = r.Next(75);
        int a = r.Next(155);
        int x1 = r.Next(100);

        // Create a bitmap and use it to create a
        // Graphics object.
        Bitmap bmp = new Bitmap(
            width, height, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bmp);

        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.Clear(Color.LightGray);

        // Use the Graphics object to draw three rectangles.
        g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
        g.DrawRectangle(Pens.Aquamarine, 2, 2, width-3, height-3);
        g.DrawRectangle(Pens.Black, 0, 0, width, height);

        // Use the Graphics object to write a string
        // on the rectangles.
        g.DrawString(
            "ASP.NET Samples", rectangleFont,
            SystemBrushes.WindowText, new PointF(10, 40));

        // Apply color to two of the rectangles.
        g.FillRectangle(
            new SolidBrush(
                Color.FromArgb(a, 255, 128, 255)),
            x, 20, 100, 50);

        g.FillRectangle(
            new LinearGradientBrush(
                new Point(x, 10),
                new Point(x1 + 75, 50 + 30),
                Color.FromArgb(128, 0, 0, 128),
                Color.FromArgb(255, 255, 255, 240)),
            x1, 50, 75, 30);

// <snippet3>    
        // Save the bitmap to the response stream and
        // convert it to JPEG format.
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);

        // Release memory used by the Graphics object
        // and the bitmap.
        g.Dispose();
        bmp.Dispose();

        // Send the output to the client.
        Response.Flush();
// </snippet3>
    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>
原文地址:https://www.cnblogs.com/CandiceW/p/4936963.html