码迷,mamicode.com
首页 > Web开发 > 详细

netty-http-response

时间:2015-03-18 12:29:38      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

在response中可以设置cookie,可以返回非html页面,如jpg或zip等。

设置cookie

response的eader中说明了浏览器需要记住哪些cookie。见下图。
技术分享
io.netty.handler.codec.http.DefaultCookie.DefaultCookie(String name, String value)
创建一个cookie。
HttpHeaders io.netty.handler.codec.http.HttpMessage.headers()
返回Http消息的头部。
HttpHeaders io.netty.handler.codec.http.HttpHeaders.set(CharSequence name, Object value)
设置头部的键值对。
CharSequence io.netty.handler.codec.http.HttpHeaders.Names.SET_COOKIE
字符串常量,内容为"Set-Cookie"。
String io.netty.handler.codec.http.ClientCookieEncoder.encode(Cookie cookie)
cookie结构体编码为字符串,以便放入httpResponse的头部。
sample
Cookie cookie=new DefaultCookie("captchaID",captcha.getCaptchaImageID());
response.headers().set(HttpHeaders.Names.SET_COOKIE, ClientCookieEncoder.encode(cookie)); 

返回图片等其他内容

1.设置头部
response.headers().set(CONTENT_TYPE, "mage/jpeg; charset=UTF-8");//说明content为图片
2.写入二进制数据
ByteBuf io.netty.buffer.ByteBuf.writeBytes(ByteBuf src)
先将byte转为ByteBuf,再通过此方法把二进制数据写入。

netty-http-response

标签:

原文地址:http://blog.csdn.net/chuchus/article/details/44407237

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!