手写服务器httpserver_准备_Socket入门_htmlJAVA195-196

本文主要是介绍手写服务器httpserver_准备_Socket入门_htmlJAVA195-196,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

来源:http://www.bjsxt.com/
一、S02E195_01手写服务器httpserver_准备_Socket入门

package com.test.server;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
/*** 创建服务器,并启动*/
public class Server {private ServerSocket server;public static void main(String[] args) {Server server = new Server();server.start();}/*** 启动方法*/public void start(){        try {server = new ServerSocket(8888);this.receive();} catch (IOException e) {e.printStackTrace();}}/*** 接收客户端*/private void receive(){try {Socket client =server.accept();StringBuilder sb =new StringBuilder();String msg =null;//接收客户端的请求信息BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));while((msg=br.readLine()).length()>0){sb.append(msg);sb.append("\r\n");if(null==msg){break;}}//接收客户端的请求信息String requestInfo =sb.toString().trim();       System.out.println(requestInfo);} catch (IOException e) {//e.printStackTrace();}}/*** 停止服务器*/public void stop(){}
}//输出信息:
GET / HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8

二、S02E196_01手写服务器httpserver_准备_html

1、get方式提交

<html><head><title>第一个表单</title></head><body><pre>method:请求方式 get/postget:默认方式,数据量小,安全性不高post:量大,安全性相对高action:请求的服务器路径id:编号,前端(用户的浏览器)区分唯一性,js中使用name:名称,后端(服务器)区分唯一性,获取值只要提交数据给后台,必须存在name</pre><form method="get" action="http://localhost:8888/index.html">用户名:<input type="text" name="uname" id="name">密码:<input type="password" name="pwd" id="pass"><input type="submit" value="登录"></form></body>
</html>

用户请求get出去

//输出信息:(第一句包含uname=intputUname&pwd=inputPassword)
GET /index.html?uname=intputUname&pwd=inputPassword HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8

2、post方式提交
(method=”get” 改为method=”post”)

<html><head><title>第一个表单</title></head><body><pre>method:请求方式 get/postget:默认方式,数据量小,安全性不高post:量大,安全性相对高action:请求的服务器路径id:编号,前端(用户的浏览器)区分唯一性,js中使用name:名称,后端(服务器)区分唯一性,获取值只要提交数据给后台,必须存在name</pre><form method="post" action="http://localhost:8888/index.html">用户名:<input type="text" name="uname" id="name">密码:<input type="password" name="pwd" id="pass"><input type="submit" value="登录"></form></body>
</html>

修改代码输出post方式的请求

package com.test.server;import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/*** 创建服务器,并启动*/
public class ServerForPost {private ServerSocket server;public static void main(String[] args) {ServerForPost server = new ServerForPost();server.start();}/*** 启动方法*/public void start(){        try {server = new ServerSocket(8888);this.receive();} catch (IOException e) {e.printStackTrace();}}/*** 接收客户端*/private void receive(){try {Socket client =server.accept();StringBuilder sb =new StringBuilder();byte[] date = new byte[20480];int len = client.getInputStream().read(date);//接收客户端的请求信息String requestInfo = new String(date,0,len).trim();     System.out.println(requestInfo);} catch (IOException e) {//e.printStackTrace();}}/*** 停止服务器*/public void stop(){}
}//输出信息:(最后一句包含uname=intputUname&pwd=inputPassword)
POST /index.html HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Content-Length: 35
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8uname=intputUname&pwd=inputPassword

这篇关于手写服务器httpserver_准备_Socket入门_htmlJAVA195-196的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1133237

相关文章

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

Python极速搭建局域网文件共享服务器完整指南

《Python极速搭建局域网文件共享服务器完整指南》在办公室或家庭局域网中快速共享文件时,许多人会选择第三方工具或云存储服务,但这些方案往往存在隐私泄露风险或需要复杂配置,下面我们就来看看如何使用Py... 目录一、android基础版:HTTP文件共享的魔法命令1. 一行代码启动HTTP服务器2. 关键参

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (