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 响应信息
已用到的方法:
![]() |
Redirect(String) |
将请求重定向到新 URL 并指定该新 URL。 |
![]() |
Redirect(String, Boolean) |
将客户端重定向到新的 URL。指定新的 URL 并指定当前页的执行是否应终止。 |
![]() |
RedirectPermanent(String) |
执行从所请求 URL 到所指定 URL 的永久重定向。 |
![]() |
RedirectPermanent(String, Boolean) |
执行从所请求 URL 到所指定 URL 的永久重定向,并提供用于完成响应的选项。 |
![]() |
RedirectToRoute(Object) |
使用路由参数值将请求重定向到新 URL。 |
![]() |
RedirectToRoute(RouteValueDictionary) |
使用路由参数值将请求重定向到新 URL。 |
![]() |
RedirectToRoute(String) |
使用路由名称将请求重定向到新 URL。 |
![]() |
RedirectToRoute(String, Object) |
使用路由参数值和路由名称将请求重定向到新 URL。 |
![]() |
RedirectToRoute(String, RouteValueDictionary) |
使用路由参数值和路由名称将请求重定向到新 URL。 |
![]() |
RedirectToRoutePermanent(Object) |
使用路由参数值执行从所请求 URL 到新 URL 的永久重定向。 |
![]() |
RedirectToRoutePermanent(RouteValueDictionary) |
使用路由参数值执行从所请求 URL 到新 URL 的永久重定向。 |
![]() |
RedirectToRoutePermanent(String) |
使用路由名称执行从所请求 URL 到新 URL 的永久重定向。 |
![]() |
RedirectToRoutePermanent(String, Object) |
使用路由参数值以及与新 URL 对应的路由的名称执行从所请求 URL 到新 URL 的永久重定向。 |
![]() |
RedirectToRoutePermanent(String, RouteValueDictionary) |
使用路由参数值和路由名称执行从所请求 URL 到新 URL 的永久重定向。 |
![]() |
ToString() |
返回表示当前对象的字符串。(从 Object 继承。) |
![]() |
TransmitFile(String) |
将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。 |
![]() |
TransmitFile(String, Int64, Int64) |
将文件的指定部分直接写入 HTTP 响应输出流,而不在内存中缓冲它。 |
![]() |
Write(Char) |
将字符写入 HTTP 响应输出流。 |
![]() |
Write(Char[], Int32, Int32) |
将字符数组写入 HTTP 响应输出流。 |
![]() |
Write(Object) |
将 Object 写入 HTTP 响应流。 |
![]() |
Write(String) |
将字符串写入 HTTP 响应输出流。 |
![]() |
WriteFile(IntPtr, Int64, Int64) |
将指定的文件直接写入 HTTP 响应输出流。 |
![]() |
WriteFile(String) |
将指定文件的内容作为文件块直接写入 HTTP 响应输出流。 |
![]() |
WriteFile(String, Boolean) |
将指定文件的内容作为内存块直接写入 HTTP 响应输出流。 |
![]() |
WriteFile(String, Int64, Int64) |
将指定的文件直接写入 HTTP 响应输出流。 |
![]() |
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", , FontStyle.Bold); // Create integer variables.
int height = ;
int width = ; // Create a random number generator and create
// variable values based on it.
Random r = new Random();
int x = r.Next();
int a = r.Next();
int x1 = r.Next(); // 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, , , width-, height-);
g.DrawRectangle(Pens.Aquamarine, , , width-, height-);
g.DrawRectangle(Pens.Black, , , width, height); // Use the Graphics object to write a string
// on the rectangles.
g.DrawString(
"ASP.NET Samples", rectangleFont,
SystemBrushes.WindowText, new PointF(, )); // Apply color to two of the rectangles.
g.FillRectangle(
new SolidBrush(
Color.FromArgb(a, , , )),
x, , , ); g.FillRectangle(
new LinearGradientBrush(
new Point(x, ),
new Point(x1 + , + ),
Color.FromArgb(, , , ),
Color.FromArgb(, , , )),
x1, , , ); // <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>
HttpResponse 类的更多相关文章
- ASP.NET -- WebForm -- HttpResponse 类的方法和属性
ASP.NET -- WebForm -- HttpResponse 类的方法和属性 1. HttpResponse 类的方法 (1) AddCacheDependency: 将一组缓存依赖项与响应关 ...
- C# 之 HttpResponse 类
Response 对象,派生自HttpResponse 类,该类封装来自 ASP.NET 操作的 HTTP 响应信息.存在于System.Web命名空间下. 注:MIME(Multipurpose I ...
- (5)ASP.NET HttpResponse 类
HttpResponse 类用来封装来自 ASP.NET 操作的 HTTP 响应信息 https://msdn.microsoft.com/zh-cn/library/system.web.httpr ...
- .net学习笔记---HttpResponse类
HttpReponse是服务器接收到浏览器的请求后,处理返回结果常用的一个类. 一.属性 Buffer 获取或设置一个值,该值指示是否缓冲输出并在处理完整个响应之后发送它. BufferOutpu ...
- HttpResponse类
HttpReponse是服务器接收到浏览器的请求后,处理返回结果常用的一个类. 一.属性 Buffer 获取或设置一个值,该值指示是否缓冲输出并在处理完整个响应之后发送它. BufferOutput ...
- Django——20141014深入理解Django HttpRequest HttpResponse的类和实例
深入理解Django HttpRequest HttpResponse的类和实例 了解META选项 了解中间件 理清所有模板传输模板变量的方式,并作出选择 Django模板系统:如何利用Django模 ...
- java http工具类和HttpUrlConnection上传文件分析
利用java中的HttpUrlConnection上传文件,我们其实只要知道Http协议上传文件的标准格式.那么就可以用任何一门语言来模拟浏览器上传文件.下面有几篇文章从http协议入手介绍了java ...
- HttpResponse的使用方法
HttpResponse的使用方法: HttpRequest类是一个封闭HTTP提交信息的类型,而封闭HTTP输出信息的类型就是HttpResponse类,使用HttpResponse类可以实现三种类 ...
- 在android 6.0(API 23)中,Google已经移除了移除了Apache HttpClient相关的类
推荐使用HttpUrlConnection,如果要继续使用需要Apache HttpClient,需要在eclipse下libs里添加org.apache.http.legacy.jar,androi ...
随机推荐
- 如何在不改SQL的情况下优化数据库
主题简介 在数据库运维中我们会遇到各种各样的问题,这些问题的根源可能很明显,也可能被某种表象掩盖而使我们认不清.所以运维面临的两大问题就是,第一我们没有看清本质,第二应用不允许修改.那么我们如何解决这 ...
- python tensorflow keras
pip install tensorflow pip install keras pip install theano http://www.open-open.com/lib/view/open14 ...
- collectionView itemW宽度计算不对
([[UIScreen mainScreen] bounds].size.width - 28) / 4.00 没加括号 collectioView不能正常撑开 用flowLayout 不要用 代理方 ...
- PhotoSwipe中文API(一)
入门 您应知道之前先做起事情: 1. PhotoSwipe不是一个简单的jQuery插件,至少基本的JavaScript知识才能安装. 2. PhotoSwipe需要预定义的图像尺寸(更多关于这一点) ...
- AbstractQueuedSynchronizer,Lock,Synchronized
Lock和Synchronized的区别 Lock实现了与synchronized相同的互斥性和内存可见性. synchronized代码简单,并且与处理异常操作实现了很好的交互. synchroni ...
- laravel常用的artisan命令
转载来源链接: https://blog.csdn.net/jiandanokok/article/details/72897682 全局篇 查看artisan命令 php artisan php a ...
- Django REST framework 之 API认证
RESTful API 认证 和 Web 应用不同,RESTful APIs 通常是无状态的, 也就意味着不应使用 sessions 或 cookies, 因此每个请求应附带某种授权凭证,因为用户授权 ...
- C#使用Gzip解压缩完整读取网页内容
using System; using System.Threading; using System.Text; using System.Text.RegularExpressions; using ...
- 安装完C++builder6.0启动的时候总是出现无法将'C:\Program Files\Borland\CBuilder6\Bin\bcb.$$$'重命名为bcb.dro
:兼容性问题 运行前右键属性“兼容性”-尝试不同的兼容性.比如“windows 8”
- 程序员:统治世界or修复bug?
程序员:统治世界or修复bug? 时至今日,我们依然生活在一个市场和技术受到高度崇拜的世界里,但是历史演化的规律提醒着我们:当一个东西开始成为社会崇拜的对象时,其中暗藏的不利因素将悄然的进行着.有人认 ...
