开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 726|回复: 9
收起左侧

[其它源码] nei网(域)认证 GET 解决方案

[复制链接]
结帖率:91% (75/82)
发表于 2024-5-9 21:06:17 | 显示全部楼层 |阅读模式   江苏省苏州市
分享源码
界面截图: -
是否带模块: -
备注说明: -
本帖最后由 z13228604287 于 2024-5-9 21:12 编辑

#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#include <iostream>
#include <tchar.h>
#include <io.h>
#include <fcntl.h>
#include <string>
#pragma comment(lib, "winhttp.lib")


// 将 UTF-8 字符串转换为 GB2312 字符串
std::string UTF8ToGB2312(const std::string& utf8Str) {
    int wideLength = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, NULL, 0);
    if (wideLength == 0) {
        return std::string();
    }

    std::wstring wideStr(wideLength, L'\0');
    MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0], wideLength);

    int gb2312Length = WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, NULL, 0, NULL, NULL);
    if (gb2312Length == 0) {
        return std::string();
    }

    std::string gb2312Str(gb2312Length, '\0');
    WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, &gb2312Str[0], gb2312Length, NULL, NULL);

    return gb2312Str;
}

// 将 GB2312 字符串转换为 UTF-8 字符串
std::string GB2312ToUTF8(const std::string& gb2312Str) {
    int wideLength = MultiByteToWideChar(CP_ACP, 0, gb2312Str.c_str(), -1, NULL, 0);
    if (wideLength == 0) {
        return std::string();
    }

    std::wstring wideStr(wideLength, L'\0');
    MultiByteToWideChar(CP_ACP, 0, gb2312Str.c_str(), -1, &wideStr[0], wideLength);

    int utf8Length = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, NULL, 0, NULL, NULL);
    if (utf8Length == 0) {
        return std::string();
    }

    std::string utf8Str(utf8Length, '\0');
    WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, &utf8Str[0], utf8Length, NULL, NULL);

    return utf8Str;
}


int main()
{


    if (WinHttpCheckPlatform()) {

         // 设置用户名和密码
        LPCWSTR username = L"****\\****";
        LPCWSTR password = L"****";

        URL_COMPONENTSW URL_Structure = { 0 };
        URL_Structure.dwStructSize = sizeof(URL_Structure);
        URL_Structure.dwSchemeLength = -1;
        URL_Structure.dwHostNameLength = -1;
        URL_Structure.dwUrlPathLength = -1;
        URL_Structure.dwExtraInfoLength = -1;

        if (!WinHttpCrackUrl(L"http://********", -1, 0, &URL_Structure))
        {
            std::wcerr << L"错误:WinHttpOpen 失败" << std::endl;
            system("pause");
            return 1;
        }
        //会话句柄  不使用代理
        HINTERNET hSession = WinHttpOpen(L"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36", WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
        if (hSession == NULL) {
            return false;
        }

        std::wstring wstrHostName = URL_Structure.lpszHostName;
        wstrHostName = wstrHostName.substr(0, URL_Structure.dwHostNameLength);
        //连接句柄
        HINTERNET hConnect = WinHttpConnect(hSession, wstrHostName.c_str(), URL_Structure.nPort, 0);
        if (hConnect == NULL) {
            WinHttpCloseHandle(hSession); return false; //创建WinHttp连接失败!
        }


        //请求句柄
        HINTERNET  hRequest = WinHttpOpenRequest(hConnect, L"GET", URL_Structure.lpszUrlPath, L"", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
        if (hRequest == NULL) {
            WinHttpCloseHandle(hSession); WinHttpCloseHandle(hConnect); return false; //创建WinHttp请求失败!
        }


        // 设置凭据指定
        if (!WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_SERVER,
            WINHTTP_AUTH_SCHEME_NTLM,
            username, password, NULL)) {
            std::cerr << "Failed to set credentials. Error: " << GetLastError() << std::endl;
            WinHttpCloseHandle(hRequest);
            WinHttpCloseHandle(hConnect);
            WinHttpCloseHandle(hSession);
            return 1;
        }


        //// 设置 Negotiate 认证系统
        //DWORD dwEnableNegotiate = WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH; // 使用 Negotiate 认证
        //if (!WinHttpSetOption(hRequest, WINHTTP_OPTION_AUTOLOGON_POLICY, &dwEnableNegotiate, sizeof(dwEnableNegotiate))) {
        //    std::cerr << "Failed to set Negotiate authentication. Error: " << GetLastError() << std::endl;
        //    WinHttpCloseHandle(hRequest);
        //    WinHttpCloseHandle(hConnect);
        //    WinHttpCloseHandle(hSession);
        //    return 1;
        //}


        PCWSTR headers = L"Host: dptwhrap1\r\n"
            L"Connection: keep-alive\r\n"
            L"Cache-Control: max-age=0\r\n"
            L"Upgrade-Insecure-Requests: 1\r\n"
            L"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.160 Safari/537.36\r\n"
            L"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\n"
            L"Accept-Encoding: gzip, deflate\r\n"
            L"Accept-Language: zh-CN,zh;q=0.9\r\n";

        // 添加请求头
        if (!WinHttpAddRequestHeaders(hRequest, headers, -1, WINHTTP_ADDREQ_FLAG_ADD)) {
            DWORD dwError = GetLastError();
            std::wcerr << L"Error: WinHttpAddRequestHeaders failed: " << dwError << std::endl;
            system("pause");

            WinHttpCloseHandle(hRequest);
            WinHttpCloseHandle(hConnect);
            WinHttpCloseHandle(hSession);
            return 1;
        }

         // 发送请求
      if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, 0, 0, 0, 0)) {
          DWORD dwError = GetLastError();
          std::wcerr << L"Error: WinHttpSendRequest failed :" << dwError << std::endl;
          system("pause");

          WinHttpCloseHandle(hRequest);
           WinHttpCloseHandle(hConnect);
          WinHttpCloseHandle(hSession);
          return 1;
      }

      // 接收响应
      if (!WinHttpReceiveResponse(hRequest, NULL)) {
          std::wcerr << L"错误:WinHttpReceiveResponse 失败" << std::endl;
          system("pause");
          WinHttpCloseHandle(hRequest);
          WinHttpCloseHandle(hConnect);
          WinHttpCloseHandle(hSession);
          return 1;
      }

      // 读取响应内容
      DWORD dwSize = 0;
      DWORD dwDownloaded = 0;
      LPBYTE pszOutBuffer;
      BOOL bResults = FALSE;
      std::string responseContent;

      // 读取响应内容
      do {
          // cha询可用数据大小
          if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
              std::wcerr << L"错误:WinHttpQueryDataAvailable 失败" << std::endl;
              system("pause");
              break;
          }

          // 如果没有可用数据,则跳出循环
          if (dwSize == 0) {
              break;
          }

          // 分配空间来存储响应内容
          pszOutBuffer = new BYTE[dwSize];
          if (!pszOutBuffer) {
              std::wcerr << L"错误:内存不足" << std::endl;
              system("pause");
              break;
          }

          // 读取响应内容
          if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) {
              std::wcerr << L"sss:WinHttpReadData 失败" << std::endl;
              system("pause");
              delete[] pszOutBuffer;
              break;
          }

          // 将读取的内容添加到字符串中
          responseContent.append(reinterpret_cast<char*>(pszOutBuffer), dwDownloaded);

          // 释放内存
          delete[] pszOutBuffer;
      } while (dwSize > 0);

      std::cout << L"data:" << std::endl<< UTF8ToGB2312(responseContent)<< std::endl;

      // 关闭句柄
      WinHttpCloseHandle(hRequest);
      WinHttpCloseHandle(hConnect);
      WinHttpCloseHandle(hSession);

      system("pause");


    }


    return 0;
}

评分

参与人数 1精币 +1 收起 理由
財財 + 1 感谢分享,很给力!~

查看全部评分


结帖率:100% (6/6)

签到天数: 20 天

发表于 2024-5-11 10:15:59 | 显示全部楼层   新疆维吾尔自治区昌吉回族自治州
谢谢分享!
回复 支持 反对

使用道具 举报

结帖率:0% (0/2)

签到天数: 16 天

发表于 2024-5-11 08:55:51 | 显示全部楼层   广西壮族自治区玉林市
感谢分享,很给力!~
回复 支持 反对

使用道具 举报

结帖率:33% (1/3)
发表于 2024-5-10 20:56:50 | 显示全部楼层   广东省深圳市
支持开源~!感谢分享
回复 支持 反对

使用道具 举报

结帖率:70% (7/10)

签到天数: 19 天

发表于 2024-5-10 09:44:20 | 显示全部楼层   河南省焦作市
谢谢分享!
回复 支持 反对

使用道具 举报

结帖率:0% (0/2)

签到天数: 16 天

发表于 2024-5-10 09:01:49 | 显示全部楼层   广西壮族自治区玉林市
感谢分享,很给力!~
回复 支持 反对

使用道具 举报

签到天数: 18 天

发表于 2024-5-10 08:42:25 | 显示全部楼层   河北省邯郸市
66666666666666666
回复 支持 反对

使用道具 举报

结帖率:80% (4/5)

签到天数: 2 天

发表于 2024-5-10 03:40:09 | 显示全部楼层   山东省潍坊市
已经顶贴,感谢您对论坛的支持!
回复 支持 反对

使用道具 举报

结帖率:100% (6/6)

签到天数: 20 天

发表于 2024-5-9 21:46:39 | 显示全部楼层   新疆维吾尔自治区昌吉回族自治州
谢谢分享!
回复 支持 反对

使用道具 举报

签到天数: 15 天

发表于 2024-5-9 21:09:31 | 显示全部楼层   广东省惠州市
好好顶贴,认真学习
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表