【计算机网络之HTTP协议详解】
家电修理 2023-07-16 19:17www.caominkang.com电器维修
超文本传输协议(Hyper Text Transfer Protocol)用于规范在网络中对文本数据的传输,属于应用层协议,底层是基于TCP/IP协议。
目录
HTTP协议的特点
HTTP报文
HTTP请求方法
HTTP的响应码
HTTP网络编程
URL
HttpURLConnection
自定义HTTP服务器
HTTP协议的特点
-
简单和快速
-
支持客户端和服务器之间的通信
-
无连接,一旦客户端完成访问后,和服务器的连接就会断开
-
无状态,服务器不会保留客户端的数据
-
采用请求和响应模式,客户端向服务器发送请求,服务器发送响应给浏览器。
HTTP报文
简单和快速
支持客户端和服务器之间的通信
无连接,一旦客户端完成访问后,和服务器的连接就会断开
无状态,服务器不会保留客户端的数据
采用请求和响应模式,客户端向服务器发送请求,服务器发送响应给浏览器。
客户端访问服务器时会发生请求报文,
格式如下
响应报文格式
HTTP请求方法
-
GET 数据会包含在URL里,不安全,对数据的长度有限制,适合于进行查询和搜索
-
POST 数据在后台发送,更加安全,对数据长度没有限制,适合于发送敏感数据
-
PUT 更新服务器资源
-
DELETE 请求删除资源
-
TRACE 跟踪服务器信息
-
OPTIONS 允许查看服务器的性能
-
HEAD 用于获取报头
-
CONNECT 将连接改为管道方式的代理服务器
HTTP的响应码
GET 数据会包含在URL里,不安全,对数据的长度有限制,适合于进行查询和搜索
POST 数据在后台发送,更加安全,对数据长度没有限制,适合于发送敏感数据
PUT 更新服务器资源
DELETE 请求删除资源
TRACE 跟踪服务器信息
OPTIONS 允许查看服务器的性能
HEAD 用于获取报头
CONNECT 将连接改为管道方式的代理服务器
服务器告诉浏览器的响应结果
常见响应码
HTTP网络编程
URL
统一资源定位系统(Uniform Resource Locator)是网络上用于指定信息位置的表示方法。
创建方法
URL url = ne URL("资源的地址");
主要方法
URLConnection openConnection() 打开网络连接
HttpURLConnection
http连接
主要方法
实现文件下载
public class DonloadDemo {
public static final String DIR = "D:\files\";
public static void donload(String path){
try {
//创建URL对象
URL url = ne URL(path);
//打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置连接超时
conn.setConnectTimeout(5000);
//请求方法
conn.setRequestMethod("GET");
//获得输入流
//创建文件输出流
String filename = UUID.randomUUID().toString().replace("-","") + ".jpg";
try(InputStream in = conn.getInputStream();
FileOutputStream out = ne FileOutputStream(DIR + filename)){
byte[] bytes = ne byte[1024];
int len = -1;
hile((len = in.read(bytes)) != -1){
out.rite(bytes,0,len);
}
}catch (IOException ex){
ex.printStackTrace();
}
System.out.println("文件下载完成");
Runtime.getRuntime().exec("mspaint " + DIR + filename);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
donload("https://gimg2.baidu./image_search/2Fb906fe02aa964cdd95bb10f106e45bcd.png");
}
}
调用后台接口
public class HttpDemo {
public static void testGet(String sUrl){
try {
URL url = ne URL(sUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(2000);
if(conn.getResponseCode() == 200){
//从输入流中读取出响应数据
BufferedReader in = ne BufferedReader(ne InputStreamReader(conn.getInputStream()));
String line = null;
hile((line = in.readLine()) != null){
System.out.println(line);
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testPost(String sUrl,String args){
try {
URL url = ne URL(sUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(2000);
conn.setRequestProperty("Content-Type","application/x--form-urlencoded");
//向服务器发送参数
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.rite(args.getBytes("UTF-8"));
if(conn.getResponseCode() == 200){
//从输入流中读取出响应数据
BufferedReader in = ne BufferedReader(ne InputStreamReader(conn.getInputStream()));
String line = null;
hile((line = in.readLine()) != null){
System.out.println(line);
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testPut(String sUrl,String args){
try {
URL url = ne URL(sUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
conn.setConnectTimeout(2000);
conn.setRequestProperty("Content-Type","application/json");
//向服务器发送参数
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.rite(args.getBytes("UTF-8"));
if(conn.getResponseCode() == 200){
//从输入流中读取出响应数据
BufferedReader in = ne BufferedReader(ne InputStreamReader(conn.getInputStream()));
String line = null;
hile((line = in.readLine()) != null){
System.out.println(line);
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testDelete(String sUrl){
try {
URL url = ne URL(sUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("DELETE");
conn.setConnectTimeout(2000);
if(conn.getResponseCode() == 200){
//从输入流中读取出响应数据
BufferedReader in = ne BufferedReader(ne InputStreamReader(conn.getInputStream()));
String line = null;
hile((line = in.readLine()) != null){
System.out.println(line);
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// testPost("http://localhost:8001/person","id=-1&name=orld&age=25&gender=男");
// testPut("http://localhost:8001/person","{"id":12,"name":"hello1","age":22,"gender":"女"}");
testDelete("http://localhost:8001/person/13");
testGet("http://localhost:8001/persons");
}
}
自定义HTTP服务器
public class HttpServer {
public static final int PORT = 8088;
public static final String DIR = "D:\html\";
//启动
public void start(){
System.out.println("启动服务器");
try(ServerSocket serverSocket = ne ServerSocket(PORT)){
hile (true){
//获得浏览器的连接
Socket socket = serverSocket.aept();
//通过IO流获得请求报文的第一行
try(BufferedReader in = ne BufferedReader(ne InputStreamReader(socket.getInputStream()));
BufferedWriter out = ne BufferedWriter(ne OutputStreamWriter(socket.getOutputStream()))){
String line = in.readLine();
//解析出url
String url = null;
if(line != null){
String[] lines = line.split(" ");
if(lines.length > 2){
url = lines[1];
}
}
//判断文件是否存在
File file = ne File(DIR + url);
if(file.exists()){
//存在,读取html代码
BufferedReader reader = ne BufferedReader(ne FileReader(file));
StringBuilder html = ne StringBuilder();
String str = null;
hile((str = reader.readLine()) != null){
html.append(str);
}
reader.close();
//发送响应报文,带html代码
StringBuilder resp = ne StringBuilder();
resp.append("HTTP/1.1 200 OKrn");
resp.append("Content-Type: text/html; charset=UTF-8rn");
resp.append("Content-Length: " + html.toString().getBytes("UTF-8").length+"rn");
resp.append("rn");
resp.append(html.toString());
out.rite(resp.toString());
}else{
//不存在就发送404响应报文
out.rite("HTTP/1.1 404 NOT FOUND");
}
out.flush();
}catch (IOException ex){
ex.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ne HttpServer().start();
}
}
空调维修
- 我的世界电脑版运行身份怎么弄出来(我的世界
- 空调抽湿是什么意思,设置抽湿的温度有什么意
- 方太燃气灶有一个打不着火 怎么修复与排查方法
- 夏季免费清洗汽车空调的宣传口号
- 清洗完空调后出现漏水现象
- iphone6能玩什么游戏(iphone6游戏)
- 如何设置电脑密码锁屏(如何设置电脑密码锁屏
- win10删除开机密码提示不符合密码策略要求
- 电脑w7显示不是正版(w7不是正版怎么解决)
- 万家乐z8热水器显示e7解决 怎么修复与排查方法
- 1匹空调多少瓦数(1匹空调多少瓦)
- 安卓手机连接电脑用什么软件好(关于安卓手机
- 电脑网页看视频卡是什么原因(爱拍看视频卡)
- 华帝燃气灶点火器一直响然后熄火怎么办:问题
- 电脑壁纸怎么换(关于电脑壁纸怎么换的介绍)
- 冬天空调的出风口应该朝什么方向(冬天空调风