深入理解万维网:URL、HTTP与HTML

深入理解万维网:URL、HTTP与HTML

  1. 统一资源定位符(URL)

1.1 什么是URL?

统一资源定位符URL(Uniform Resource Locator)是万维网上用于标识和定位各种文档的标准方法,它使每个资源在互联网范围内具有唯一标识。

示例URL:

复制代码
https://www.makeru.com.cn:443/index.html

1.2 URL语法结构

复制代码
scheme://host.domain:port/path/filename

• scheme - 定义因特网服务类型

• host - 定义域主机(默认www)

• domain - 定义因特网域名

• port - 定义主机端口号

• path - 定义服务器路径

• filename - 定义资源名称

1.3 常见URL Scheme

Scheme 描述 用途
http 超文本传输协议 普通网页,不加密
https 安全超文本传输协议 加密的安全网页
ftp 文件传输协议 文件上传下载
file 本地文件协议 访问本地文件
  1. HTTP与HTTPS协议

2.1 HTTP协议基础

HTTP(Hypertext Transfer Protocol)是用于分布式、协作式超媒体信息系统的应用层协议。

特点:

• 基于TCP/IP通信协议

• 默认端口80

• 无状态协议

• 使用请求-响应模型

2.2 HTTPS协议

HTTPS(Hypertext Transfer Protocol Secure)是HTTP的安全版本:

• 添加SSL/TLS加密层

• 默认端口443

• 提供数据加密、完整性校验和身份验证

2.3 HTTP请求-响应流程

  1. 建立TCP连接

  2. 客户端发送HTTP请求

  3. 服务器处理请求

  4. 服务器返回HTTP响应

  5. 关闭连接(HTTP/1.1默认保持连接)

  6. HTTP消息结构

3.1 HTTP请求消息

复制代码
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/html

3.2 HTTP响应消息

复制代码
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache
Content-Type: text/html
Content-Length: 143

<!DOCTYPE html>
<html>
...
</html>
  1. HTTP方法
方法 描述
GET 获取资源
POST 提交数据
PUT 更新资源
DELETE 删除资源
HEAD 获取头部信息
OPTIONS 查询服务器支持的方法
  1. HTTP状态码

5.1 状态码分类

分类 描述
1xx 信息响应
2xx 成功响应
3xx 重定向
4xx 客户端错误
5xx 服务器错误

5.2 常见状态码

状态码 描述
200 请求成功
301 永久重定向
404 资源未找到
500 服务器内部错误
  1. HTML基础

6.1 HTML文档结构

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>页面标题</title>
</head>
<body>
    <h1>我的第一个标题</h1>
    <p>我的第一个段落。</p>
</body>
</html>

6.2 常见HTML元素

<h1>-<h6>:标题

<p>:段落

<a>:超链接

<img>:图像

<div>:块级容器

<span>:行内容器

  1. 使用Socket实现HTTP通信

7.1 基础Socket实现

c 复制代码
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
    // 创建套接字
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    
    // 绑定地址
    struct sockaddr_in addr = {
        .sin_family = AF_INET,
        .sin_port = htons(8080),
        .sin_addr.s_addr = INADDR_ANY
    };
    bind(fd, (struct sockaddr *)&addr, sizeof(addr));
    
    // 监听并接受连接
    listen(fd, 5);
    int cfd = accept(fd, NULL, NULL);
    
    // 处理请求
    char buf[BUFSIZ];
    recv(cfd, buf, BUFSIZ, 0);
    printf("%s\n", buf);
    
    // 关闭连接
    close(cfd);
    close(fd);
    return 0;
}

7.2 处理GET请求

c 复制代码
int do_get(int fd, char *buf, size_t len) {
    char type[16], resource[16];
    sscanf(buf, "%s%s\n", type, resource);
    
    if(strncasecmp("GET", type, 3)) {
        send(fd, "HTTP/1.1 501 Not Implemented\r\n\r\n", 29, 0);
        return -1;
    }
    
    char *pathname = (strlen(resource) == 1 && resource[0] == '/') ? 
                    "index.html" : &resource[1];
    
    FILE *fp = fopen(pathname, "r");
    if(!fp) {
        send(fd, "HTTP/1.1 404 Not Found\r\n\r\n", 26, 0);
        return -1;
    }
    
    char response[BUFSIZ];
    fread(response, 1, len, fp);
    
    char header[1024];
    sprintf(header, "HTTP/1.1 200 OK\r\n"
                   "Content-Length: %ld\r\n"
                   "Content-Type: text/html\r\n\r\n", strlen(response));
    
    send(fd, header, strlen(header), 0);
    send(fd, response, strlen(response), 0);
    
    fclose(fp);
    return 0;
}
相关推荐
YMWM_2 小时前
UDP协议详解:从原理到Python实践
网络·网络协议·udp
pengyi8710152 小时前
共享 IP 与独享 IP 怎么选?被封后升级方案避坑
网络·网络协议·tcp/ip
半壶清水5 小时前
用P4 Tutorial、BMv2 和 Mininet‌解析网络第一集------模拟环境搭建
运维·服务器·网络·网络协议·tcp/ip
BullSmall5 小时前
Promtheus和Alertmanager 之间是通过管理平面还是业务层面IP交互
网络协议·tcp/ip·平面
DONSEE广东东信智能读卡器6 小时前
用PowerShell实现Windows 本地 WSS/HTTPS 自签名证书配置方法
windows·网络协议·https·powershell·身份证阅读器
2501_916007476 小时前
iOS开发中抓取HTTPS请求的完整解决方法与步骤详解
android·网络协议·ios·小程序·https·uni-app·iphone
Irissgwe6 小时前
一、网络基础概念
linux·网络·websocket·网络协议·socket·linux网络编程
treesforest7 小时前
2026年,IP地理位置精准查询的几个硬核技术变化
运维·网络·网络协议·tcp/ip·ip
yqcoder8 小时前
数据的“包装方式”:深入解析 HTTP Content-Type
网络·网络协议·http
pengyi87101510 小时前
共享 IP 防封维护策略,降低被封率、延长 IP 寿命
网络·网络协议·tcp/ip