跳到正文

jinky111

1397697

这个家伙一点也不懒,但也什么也没有留下

在洛谷打开
资料、奖项与版本

资料

编号
1397697
抓取
C++
#define UNICODE
#include <windows.h>
#include <wininet.h>
#include <fstream>
// 手动定义,旧版 SDK 中缺少此宏
#ifndef INTERNET_COOKIE_HTTPONLY
#define INTERNET_COOKIE_HTTPONLY 0x00002000
#endif
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

#pragma comment(lib, "wininet.lib")

// ============================================================
// 鏂规涓€锛氱洿鎺ヤ粠 HTTP 鍝嶅簲澶存彁鍙?Set-Cookie锛堟渶鍙潬锛?
// ============================================================
std::wstring GetCookiesFromResponseHeaders(HINTERNET hUrl) {
    std::wstring allCookies;

    // 鍏堣幏鍙栨墍鏈夊搷搴斿ご鐨勫ぇ灏?
    DWORD headerSize = 0;
    HttpQueryInfoW(hUrl, HTTP_QUERY_SET_COOKIE, NULL, &headerSize, NULL);

    if (GetLastError() == ERROR_INSUFFICIENT_BUFFER && headerSize > 0) {
        // 鍙兘鏈夊涓?Set-Cookie 澶达紝鐢?index 閬嶅巻
        DWORD index = 0;
        while (true) {
            std::vector<wchar_t> buffer(headerSize / sizeof(wchar_t) + 1);
            DWORD bufSize = headerSize;
            BOOL ok = HttpQueryInfoW(
                hUrl,
                HTTP_QUERY_SET_COOKIE,
                buffer.data(),
                &bufSize,
                &index
            );
            if (!ok || bufSize == 0) break;
            allCookies += buffer.data();
            allCookies += L"\n";
        }
    }
    return allCookies;
}

// ============================================================
// 鏂规浜岋細鐢?InternetGetCookieExW 鑾峰彇宸茬紦瀛樼殑 Cookie
// 鏀寔 HTTPONLY Cookie
// ============================================================
std::wstring GetCachedCookies(const std::wstring& url) {
    DWORD dataSize = 0;
    BOOL result = InternetGetCookieExW(
        url.c_str(),
        NULL,
        NULL,
        &dataSize,
        INTERNET_COOKIE_HTTPONLY,  // 0x2000, 鍖呮嫭 HttpOnly Cookie
        NULL
    );

    if (result && dataSize > 0) {
        std::vector<wchar_t> buffer(dataSize);
        result = InternetGetCookieExW(
            url.c_str(),
            NULL,
            buffer.data(),
            &dataSize,
            INTERNET_COOKIE_HTTPONLY,
            NULL
        );
        if (result) {
            return std::wstring(buffer.data());
        }
    }

    // 涔熷皾璇曚笉甯?HTTPONLY 鐨勬櫘閫?InternetGetCookieW
    dataSize = 0;
    result = InternetGetCookieW(url.c_str(), NULL, NULL, &dataSize);
    if (result && dataSize > 0) {
        std::vector<wchar_t> buffer(dataSize);
        result = InternetGetCookieW(url.c_str(), NULL, buffer.data(), &dataSize);
        if (result) {
            return std::wstring(buffer.data());
        }
    }

    return L"";
}

// ============================================================
// 鑾峰彇鏈€缁堢殑 URL锛堝鐞嗛噸瀹氬悜鍚庣殑鐪熷疄鍦板潃锛?
// ============================================================
std::wstring GetFinalUrl(HINTERNET hInternet, const std::wstring& originalUrl) {
    HINTERNET hUrl = InternetOpenUrlW(
        hInternet,
        originalUrl.c_str(),
        NULL, 0,
        INTERNET_FLAG_NO_UI | INTERNET_FLAG_RELOAD,
        0
    );
    if (!hUrl) return originalUrl;

    // 璇诲彇鍝嶅簲浣擄紙瑙﹀彂 Cookie 缂撳瓨锛?
    char buffer[4096];
    DWORD bytesRead;
    while (InternetReadFile(hUrl, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) {}

    // 鑾峰彇鏈€缁?URL
    wchar_t urlBuf[2048];
    DWORD urlBufSize = sizeof(urlBuf);
    DWORD infoSize = urlBufSize;
    if (HttpQueryInfoW(hUrl, HTTP_QUERY_CUSTOM, urlBuf, &infoSize, NULL)) {
        // HTTP_QUERY_CUSTOM 涓嶄竴瀹氳兘鎷垮埌 URL
    }

    // 灏濊瘯閫氳繃 INTERNET_OPTION_URL 鑾峰彇
    DWORD dwUrlLen = sizeof(urlBuf);
    if (InternetQueryOptionW(hUrl, INTERNET_OPTION_URL, urlBuf, &dwUrlLen)) {
        InternetCloseHandle(hUrl);
        return std::wstring(urlBuf);
    }

    InternetCloseHandle(hUrl);
    return originalUrl;
}

int main() {
    std::wstring url;
	std::wofstream wfout("cookie.txt");
    std::wcout << L"Please enter the URL (e.g., https://www.luogu.com.cn): ";
    std::getline(std::wcin, url);

    if (url.empty()) {
        std::wcout << L"URL cannot be empty!" << std::endl;
        return 1;
    }

    // ---- Step 1: 鎵撳紑 Internet 鍙ユ焺 ----
    HINTERNET hInternet = InternetOpenW(
        L"MyCookieApp",
        INTERNET_OPEN_TYPE_DIRECT,
        NULL, NULL, 0
    );
    if (!hInternet) {
        std::wcout << L"InternetOpen failed. Error: " << GetLastError() << std::endl;
        return 1;
    }

    // ---- Step 2: 璁块棶鐩爣 URL ----
    std::wcout << L"\n[Step 1] Fetching URL to populate cookie cache..." << std::endl;
    HINTERNET hUrl = InternetOpenUrlW(
        hInternet,
        url.c_str(),
        NULL, 0,
        INTERNET_FLAG_NO_UI | INTERNET_FLAG_RELOAD,
        0
    );

    if (!hUrl) {
        std::wcout << L"InternetOpenUrl failed. Error: " << GetLastError() << std::endl;
        InternetCloseHandle(hInternet);
        return 1;
    }

    // 璇诲彇鍝嶅簲浣擄紙瑙﹀彂 Cookie 缂撳瓨锛?
    std::string responseBody;
    char buffer[4096];
    DWORD bytesRead;
    while (InternetReadFile(hUrl, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) {
        responseBody.append(buffer, bytesRead);
    }
    std::wcout << L"Response body size: " << responseBody.size() << L" bytes" << std::endl;

    // ---- Step 3: 浠庡搷搴斿ご鐩存帴鎻愬彇 Set-Cookie ----
    std::wcout << L"\n[Step 2] Extracting Set-Cookie from response headers..." << std::endl;
    std::wstring responseCookies = GetCookiesFromResponseHeaders(hUrl);

    if (!responseCookies.empty()) {
        std::wcout << L"\n=== Cookies from Response Headers ===" << std::endl;
        std::wcout << responseCookies << std::endl;
    } else {
        std::wcout << L"No Set-Cookie headers found in response." << std::endl;
    }

    InternetCloseHandle(hUrl);

    // ---- Step 4: 灏濊瘯浠?WinINet Cookie 缂撳瓨鑾峰彇 ----
    // 浣跨敤澶氱 URL 鍙樹綋灏濊瘯锛堝鐞嗛噸瀹氬悜闂锛?
    std::wcout << L"\n[Step 3] Trying to get cached cookies..." << std::endl;

    // 灏濊瘯鍘熷 URL
    std::wstring cachedCookies = GetCachedCookies(url);

    // 濡傛灉鍘熷 URL 娌℃壘鍒帮紝灏濊瘯甯歌鐨勯噸瀹氬悜鍙樹綋
    if (cachedCookies.empty()) {
        // http -> https
        if (url.find(L"http://") == 0) {
            std::wstring httpsUrl = L"https://" + url.substr(7);
            cachedCookies = GetCachedCookies(httpsUrl);
            if (!cachedCookies.empty()) {
                std::wcout << L"(Found via HTTPS variant: " << httpsUrl << L")" << std::endl;
            }
        }
    }
    if (cachedCookies.empty()) {
        // 鍔?www
        std::wstring wwwUrl = url;
        size_t schemeEnd = wwwUrl.find(L"://");
        if (schemeEnd != std::wstring::npos) {
            std::wstring scheme = wwwUrl.substr(0, schemeEnd + 3);
            std::wstring rest = wwwUrl.substr(schemeEnd + 3);
            if (rest.find(L"www.") != 0) {
                wwwUrl = scheme + L"www." + rest;
                cachedCookies = GetCachedCookies(wwwUrl);
                if (!cachedCookies.empty()) {
                    std::wcout << L"(Found via WWW variant: " << wwwUrl << L")" << std::endl;
                }
            }
        }
    }
    if (cachedCookies.empty()) {
        // https + www
        if (url.find(L"http://") == 0) {
            std::wstring httpsWwwUrl = L"https://www." + url.substr(7);
            // 鍘绘帀宸叉湁鐨?www锛堝鏋滄湁锛?
            std::wstring rest = url.substr(7);
            if (rest.find(L"www.") == 0) {
                httpsWwwUrl = L"https://" + rest;
            }
            cachedCookies = GetCachedCookies(httpsWwwUrl);
            if (!cachedCookies.empty()) {
                std::wcout << L"(Found via HTTPS+WWW variant: " << httpsWwwUrl << L")" << std::endl;
            }
        }
    }

    if (!cachedCookies.empty()) {
        std::wcout << L"\n=== Cached Cookies ===" << std::endl;
        std::wcout << cachedCookies << std::endl;
        wfout << cachedCookies << std::endl;
    } else {
        std::wcout << L"No cached cookies found for any URL variant." << std::endl;
        std::wcout << L"\nThis is normal if the server did not send any Set-Cookie headers." << std::endl;
        wfout << L"No cached cookies found for any URL variant." << std::endl;
        wfout << L"\nThis is normal if the server did not send any Set-Cookie headers." << std::endl;
    }

    // ---- Step 5: 鑾峰彇鏈€缁堥噸瀹氬悜鍚庣殑 URL 淇℃伅 ----
    std::wcout << L"\n[Step 4] Getting final redirected URL..." << std::endl;
    std::wstring finalUrl = GetFinalUrl(hInternet, url);
    std::wcout << L"Final URL: " << finalUrl << std::endl;

    if (finalUrl != url) {
        std::wcout << L"\nNote: The URL was redirected!" << std::endl;
        std::wcout << L"Original: " << url << std::endl;
        std::wcout << L"Final:    " << finalUrl << std::endl;

        // 鐢ㄦ渶缁?URL 鍐嶈瘯涓€娆?
        std::wstring finalCookies = GetCachedCookies(finalUrl);
        if (!finalCookies.empty()) {
            std::wcout << L"\n=== Cookies for Final URL ===" << std::endl;
            std::wcout << finalCookies << std::endl;
        }
    }

    InternetCloseHandle(hInternet);
    std::wcout << L"\nDone." << std::endl;
    return 0;
}

共 7 篇
  1. 闲话

    传输文件点

    该C++程序是一个基于Windows的自动化脚本工具,主要功能如下: 核心功能 通过热键控制程序行为: F8:将光标移至预设关闭坐标并模拟鼠标左键点击(若MC模式开启则先按Esc) Tab:将光标移至预设最小化坐标并模拟鼠标左键点击 F4:记录当前光标坐标为关闭坐标 F3:记录当前光标坐标为最小化坐标 F2:切换MC模式(开启/关闭) F5:切换关闭程序功能(开启/关闭) 运行逻辑 持续检测按键状态,每80毫秒清屏并输出当前坐标、预设坐标及功能状态 若检测到F8或Tab触发后,若F5功能开启则程序自动退出 技术实现 使用Windows API实现鼠标操作(SetCursorPos、mouse_event) 通过conio.h的_getch_函数检测按键状态 采用循环监听机制,确保持续运行和实时响应 程序主要用于自动化窗口操作,可能应用于游戏辅助或界面控制场景。

  2. 科技·工程

    国际象棋 Minimax博弈AI(DP搜索)

    该文本是一个基于HTML和JavaScript实现的国际象棋人机对战游戏代码,核心功能包括: 棋盘布局与交互:使用CSS Grid创建8x8棋盘,支持双人对战/人机对战模式(玩家为白棋,AI为黑棋),包含移动选择、升变棋子(皇后/车/马/象)和胜负判定功能。 Minimax算法实现:AI通过递归搜索(最大深度3)计算最佳移动,采用alpha-beta剪枝优化性能,结合棋子价值评估函数(K=100000, Q=900, R=500等)和棋子位置权重表(如兵的PAWN_TABLE)进行局面评估。 规则支持:包含将杀检测、三次重复局面判定(和棋)、王车易位规则、吃过路兵规则等国际象棋核心规则的实现。 界面交互:通过模态框实现升变选择,游戏结束时弹出胜负提示,支持重新开局功能。

  3. 题解

    7.19最小生成树测试题解

    本文是一份关于 7 月 19 日最小生成树(MST)竞赛测试的题解文档,主要包含以下内容: 整体概述:涵盖 A、B、C 三道题目的解题思路与 C++ 代码实现,D 题仅列出标题无具体内容。 核心算法:所有题目均采用 Kruskal 算法 结合 并查集(Union-Find) 来求解最小生成树。 题目细节: A 题 (Watering the Fields):根据坐标计算点间距离平方作为边权,需过滤掉小于阈值 c 的边。 B 题 (Out of Hay):在构建 MST 的过程中记录所选边的最大权重。 C 题 (Jungle Roads):需将节点名称(字母)转换为数字索引,且支持多组测试数据输入。 代码特点:均使用了 ios::sync_with_stdio(false) 进行输入输出加速,并实现了并查集的查找与合并操作。

  4. 学习·文化课

    6.14测试总结

    T619145 质数筛 T619147 陶陶摘苹果 T619148 单词的长度 T619149 迷宫出口 T619150 选数 T619151 海战 T619153 全排列问题

  5. 学习·文化课

    1.11考后总结

    jinky111的总结 最长上升子序列| 【深基5.习7】杨辉三角 一次 思路:第1第2排为1 ,从第三排开始f[i]=f[i-1][j]+f[i-1][j-1] 也就是正上和左上相加。 代码: 最长上升子序列 依然一次 思路:在a[i]>a[j]的时候看之前的最长上升子序列,并…