分类: 技术学习

  • HTTP状态码全指南

    一、状态码分类逻辑

    HTTP状态码按首位数字分为5类,构成Web通信的「响应语言」:

    • 1xx:信息性状态(协议处理中)
    • 2xx:成功状态(请求已达成)
    • 3xx:重定向状态(资源位置变更)
    • 4xx:客户端错误(请求不合法)
    • 5xx:服务端错误(服务器处理失败)

    这种分类方式让开发者能快速定位请求问题的大致方向,极大提升调试效率。

    二、全量状态码速查表

    1xx Informational(信息响应)

    状态码 名称 典型场景 补充说明
    100 Continue 客户端应继续发送请求体 常用于大文件上传,先确认服务器接收意向
    101 Switching Protocols 服务器同意升级协议(如WebSocket) 需在请求头中指定Upgrade字段
    102 Processing 服务器正在处理但未完成 多用于WebDAV协议下的复杂操作

    2xx Success(成功响应)

    状态码 名称 关键特性 常见应用场景
    200 OK 标准成功响应 GET请求成功返回数据
    201 Created 资源创建成功(POST返回新URL) 接口创建用户、商品等资源时使用
    202 Accepted 请求已接收但未处理完 异步任务提交(如文件上传排队)
    204 No Content 响应体为空(如DELETE成功) 删除资源后,减少不必要的数据传输

    3xx Redirection(重定向)

    状态码 名称 缓存行为 方法保留规则 适用场景
    301 Moved Permanently 永久缓存 GET可能变HEAD 网站域名更换、页面永久迁移
    302 Found 临时缓存 方法可能改变 登录成功后跳转首页
    307 Temporary Redirect 不缓存 强制保留原始方法 临时维护页面跳转
    308 Permanent Redirect 永久缓存 强制保留原始方法 API接口版本永久变更

    4xx Client Error(客户端错误)

    状态码 名称 高频触发场景 修复建议
    400 Bad Request 请求语法错误 检查参数格式、请求头完整性
    401 Unauthorized 未提供有效身份凭证 添加认证信息(如Token、Basic Auth)
    403 Forbidden 权限不足(如访问私有文件) 确认用户角色权限配置
    404 Not Found 资源不存在 检查URL路径或资源删除逻辑
    429 Too Many Requests 触发速率限制 调整请求频率或申请更高配额

    5xx Server Error(服务端错误)

    状态码 名称 故障类型 排查方向
    500 Internal Server Error 未分类的服务器错误 检查服务器日志、代码异常捕获
    502 Bad Gateway 上游服务器无响应 确认网关配置、后端服务健康状态
    503 Service Unavailable 主动停机维护/过载 查看维护公告、扩容服务器资源
    504 Gateway Timeout 上游服务器响应超时 优化网络配置、增加超时重试机制

    三、关键场景实战指南

    1. SEO优化组合拳

    • 301+308:永久迁移时保留链接权重
    • 429+Retry-After:应对爬虫时友好限流

    2. API设计黄金法则

    GET /api/users/1 HTTP/1.1
    -> 200 OK(成功)
    -> 404 Not Found(资源不存在)
    -> 410 Gone(资源已删除且无新地址)

    3. 错误处理最佳实践

    • 4xx错误必须返回清晰错误详情(如JSON Body)
    • 5xx错误应记录完整日志链(Request-ID追踪)

    四、冷知识彩蛋

    ​​418 I’m a teapot​​:源自HTTP愚人节RFC(真实存在于某些库中)
    ​​206 Partial Content​​:支持断点续传的核心状态码

  • 一行代码没写,我通过ai搭建了个网站

    收藏夹是我强行称呼的。其实就是一个列表。你可以访问 linkpark.site 。实现效果如下:

    _20241207133002.png

    实现的效果:

    1. 读取我手动维护的静态文件(markdown文件)来生成网页列表
    2. 列表搜索
    3. 随机跳转某个Link的按钮
    4. 在随机列表获取一张图片作为背景图
    5. 获取到背景图的主要颜色设置为按钮的配色

    如果你对这个内容有兴趣,你可以参考一下下面的代码:

    <!DOCTYPE html>
    <html lang="zh">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LINKPARK.SITE</title>
    <link rel="icon" href="./icon.png">
    <style>
            body {
                font-family:  "Ubuntu","Noto Sans SC",sans-serif;
                background-color: #f4f4f4;
                margin: 0;
                display: flex;
                justify-content: center;
                align-items: center;
            }
            .container {
                max-width: 800px;
                background-color: rgba(255, 255, 255, 0.1);
                border-radius: 12px;
                backdrop-filter: blur(10px);
                padding: 30px;
                margin: 0;
            }
            h1 {
                text-align: center;
                color: #333;
                margin-bottom: 10px;
            }
            input[type="text"] {
                box-sizing: border-box;
                width: 100%; 
                padding: 12px;
                border: 1px solid #ccc;
                border-radius: 6px;
                margin: 0 auto 20px; 
                font-size: 16px;
                display: block; 
                transition: border-color 0.3s;
            }
            input[type="text"]:focus {
                border-color: #555;
                outline: none;
            }
            .button-container {
            border: medium; /* 去掉边框 */
            border-radius: 4px;
            background-color: #EEEEEE; 
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            color: #fff;
            }
            button {
            flex: 1; /* 使按钮平分可用空间 */
            margin:  10px; /* 添加左右间距 */
            padding: 10px;
            color: #fff; /* 按钮文字颜色 */
            border: none; /* 去掉边框 */
            border-radius: 4px; /* 圆角边框 */
            cursor: pointer; /* 鼠标悬停时变成手指图标 */
            transition: background-color 0.3s; /* 添加过渡效果 */
            }
            button:hover {
            background-color: #0056b3; /* 悬停时变色 */
            color: #fff;
            }
            ul {
                list-style-type: none;
                padding: 0;
            }
            li {
                display: flex;
                flex-direction: column;
                position: relative;
                overflow: hidden;
                margin: 10px 0;
                border-radius: 6px;
                background-color: #f9f9f9; 
                transition: background-color 0.3s, transform 0.3s;
                padding: 15px;
                box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            }
            li:hover {
                background-color: #eeeeee;
                transform: translateY(-2px);
            }
            img {
                width: 20px; 
                height: 20px; 
                margin-right: 10px; 
            }
            .subtitle {
                color: #888; 
                font-size: 14px; 
                margin-top: 5px; 
            }
            .title {
                font-size: 18px;
                font-weight: 600;
            }
            a {
                text-decoration: none;
                color: inherit;
                display: block; 
                height: 100%; 
            }
            @media (min-width: 768px) {
                .container {
                    margin: 5vh 0; 
                }
            }
        </style>
    <script type="module" src="https://linkpark.site/markdown.js" onload="loadMarkdown()"></script>
    </head>
    <body>
    <div class="container">
    <h1>linkpark<span style="color:#007bff">.</span>site</h1>
    <p style="color:#333;padding: 5px;word-break: break-all; font-size:12px">本页面为个人收藏列表页。从未有意冒用各网站商标,如果您认为您的内容或权益受到侵犯,请联系 m@linkpark.site 。本页面不收集任何信息。</p>
    <input type="text" id="search" placeholder="搜索列表项..." oninput="filterList()">
    <!-- 按钮容器 -->
    <div class="button-container">
    <button onclick="setSearchValue('划水')">划水</button>
    <button onclick="setSearchValue('设计')">设计</button>
    <button onclick="setSearchValue('ai')">ai</button>
    <button onclick="setSearchValue('开发')">开发</button>
    <button id="randomButton">无聊</button>
    </div>
    
    <hr style="border-style: dashed; border-color: #ccc;">
    <ul id="itemList"></ul>
    </div>
    
    <canvas id="colorCanvas" style="display:none;"></canvas>
    
    <script>
        
        async function loadMarkdown() {
        try {
            const response = await fetch('https://linkpark.site/links.md');
            if (!response.ok) {
                throw new Error('网络响应不是 OK');
            }
            const text = await response.text();
            const html = marked.parse(text);
            
            const container = document.createElement('div');
            container.innerHTML = html;
    
            const table = container.querySelector('table');
            const itemList = document.getElementById('itemList');
            itemList.innerHTML = '';
    
            if (table) {
                const rows = table.querySelectorAll('tr');
    
                for (let i = 1; i < rows.length; i++) {
                    const cols = rows[i].querySelectorAll('td');
                    if (cols.length >= 4) {
                        const imgSrc = cols[0].querySelector('img')?.src; 
                        const title = cols[1].innerText; 
                        const description = cols[2].innerText; 
                        const link = cols[3].querySelector('a')?.href; 
    
                        // 将 <a> 标签放在 <li> 标签外部
                        const newLi = document.createElement('li');
                        newLi.innerHTML = `
                                <div style="display: flex; align-items: center;">
                                    <img src="${imgSrc}" alt="icon">
                                    <span class="title">${title}</span>
                                </div>
                            <div class="subtitle">${description}</div>
                        `;
                        
                        // 创建链接并将其包裹在 <li> 外部
                        const anchor = document.createElement('a');
                        anchor.href = link;
                        anchor.appendChild(newLi);
                        itemList.appendChild(anchor);
                    }
                }
            }
        } catch (error) {
            console.error('加载 Markdown 失败:', error);
        }
    }
    
    
    function filterList() {
        const searchInput = document.getElementById('search').value.toLowerCase();
        const items = document.querySelectorAll('#itemList li');
    
        items.forEach(item => {
            const text = item.textContent.toLowerCase();
            item.style.display = text.includes(searchInput) ? '' : 'none';
        });
    }
    
    // 设置搜索框的值
    function setSearchValue(value) {
        document.getElementById('search').value = value;
        filterList(); // 更新过滤列表
    }
    
    function redirectTo(url) {
        window.location.href = url;
    }
    
            // 图片链接列表
            const imageUrls = [
                'https://linkpark.site/1.jpg',
                'https://linkpark.site/2.jpg',
                'https://linkpark.site/3.jpg',
                'https://linkpark.site/4.jpg',
                'https://linkpark.site/5.jpg',
                'https://linkpark.site/6.jpg',
                'https://linkpark.site/7.jpg',
                'https://linkpark.site/8.jpg',
                'https://linkpark.site/9.jpg',
                'https://linkpark.site/10.jpg'
            ];
    
            // 随机选择一个链接
            const randomImageUrl = imageUrls[Math.floor(Math.random() * imageUrls.length)];
            document.body.style.background = `url('${randomImageUrl}') no-repeat center center fixed`;
            document.body.style.backgroundSize = 'cover'; // 让背景图覆盖整个屏幕
    
            const canvas = document.getElementById('colorCanvas');
            const ctx = canvas.getContext('2d');
    
            function getDominantColor(image) {
                const width = image.width;
                const height = image.height;
                canvas.width = width;
                canvas.height = height;
                ctx.drawImage(image, 0, 0, width, height);
    
                const imageData = ctx.getImageData(0, 0, width, height);
                const data = imageData.data;
    
                let r = 0, g = 0, b = 0, count = 0;
    
                for (let i = 0; i < data.length; i += 4) {
                    r += data[i];     // Red
                    g += data[i + 1]; // Green
                    b += data[i + 2]; // Blue
                    count++;
                }
    
                r = Math.floor(r / count);
                g = Math.floor(g / count);
                b = Math.floor(b / count);
    
                return `rgb(${r}, ${g}, ${b})`;
            }
    
            const img = new Image();
            img.crossOrigin = "Anonymous"; // 解决跨域问题
            img.src = randomImageUrl; // 使用随机选择的图片链接
            img.onload = () => {
                const dominantColor = getDominantColor(img);
                document.querySelectorAll('button').forEach(button => {
                    button.style.backgroundColor = dominantColor; // 设置按钮背景色
                    button.style.color = '#ffffff'; // 设置按钮文字颜色为白色
                });
            };
    </script>
    <script type="module" src="https://linkpark.site/linkpark.js""></script>
    </body>
    </html>
  • 对跖点

    其实很久之前也会有一闪而过的想法,地球另一端的人不知道是谁,他的生活是怎么样的。昨天在网上看视频看到了这个概念。只需要简单地计算就可以知道地球上离你最远地点在哪里。

    (更多…)