javawebserver-封装响应协议-创新互联
Response:
创新互联公司是一家专业提供涧西企业网站建设,专注与成都网站制作、成都做网站、外贸营销网站建设、H5响应式网站、小程序制作等业务。10年已为涧西众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。public class Response {
private BufferedWriter bw;
private Socket client;
private StringBuilder headInfo; //协议头包括状态行和请求头和回车
private StringBuilder content;
private int len=0; //正文的字节数
private final String BLANK=" ";
private final String CRLF="\r\n";
private Response()
{
content=new StringBuilder();
headInfo=new StringBuilder();
len=0;
}
public Response(Socket client)
{
this(); //调用默认构造器
try {
bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
headInfo=null;
}
}
public Response(OutputStream os)
{
this(); //调用默认构造器
bw=new BufferedWriter(new OutputStreamWriter(os));
}
public Response print(String info) //流模式,不断添加内容返回自身
{
content.append(info);
len+=info.getBytes().length;
return this;
}
public Response println(String info)
{
content.append(info).append(CRLF);
len+=(info+CRLF).getBytes().length; //换行符也占长度
return this;
}
//推送响应信息
public void pushToBrowser(int code) throws IOException
{
if(null==headInfo) {
code=505;
}
createHeadInfo(code);
bw.append(headInfo); //bw.append()以追加模式写出数据,.write()刷新原有数据,只有当前数据
bw.append(content);
bw.flush();
}
//构建头信息
private void createHeadInfo(int code) //传不同的code有不同的状态
{
//1、响应行:HTTP/1.1 200 OK
headInfo.append("HTTP/1.1").append(BLANK);
headInfo.append(code).append(BLANK);
switch(code)
{
case 200:
headInfo.append("OK").append(CRLF);
break;
case 404:
headInfo.append("NOT FOUND").append(CRLF);
break;
case 505:
headInfo.append("SERVER ERROR").append(CRLF);
break;
}
//2、响应头
headInfo.append("Date:").append(new Date()).append(CRLF);
headInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
headInfo.append("Content-type:text/html").append(CRLF);
headInfo.append("Content-length:").append(len).append(CRLF);
headInfo.append(CRLF); //响应头和正文之间有空行
}
}
Server:
public class Server02 {
private ServerSocket serverSocket ;
public static void main(String[] args) {
Server02 server = new Server02();
server.start();
}
//启动服务
public void start() {
try {
serverSocket = new ServerSocket(8888);
receive();
} catch (IOException e) {
e.printStackTrace();
System.out.println("服务器启动失败....");
}
}
//接受连接处理
public void receive() {
try {
Socket client = serverSocket.accept();
System.out.println("一个客户端建立了连接....");
//获取请求协议
InputStream is =client.getInputStream();
byte[] datas = new byte[1024*1024];
int len = is.read(datas);
String requestInfo = new String(datas,0,len);
System.out.println(requestInfo);
//关注了内容
Response response=new Response(client); //创建好了输出流
response.print(""); //通过输出流输出
response.print("");
response.print("");
response.print("服务器响应成功");
response.print(" ");
response.print("");
response.print("");
response.print("shsxt server终于回来了。。。。");
response.print("");
response.print("");
//关注了状态码
response.pushToBrowser(200);
} catch (IOException e) {
e.printStackTrace();
System.out.println("客户端错误");
}
}
//停止服务
public void stop() {
}
}
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
文章题目:javawebserver-封装响应协议-创新互联
网页网址:http://myzitong.com/article/doiihh.html