C++ LibCurl 发送 HTTPS 请求

发布于 2024-10-03 11:38:27 字数 1382 浏览 3 评论 0原文

我有一个插件,我已经使用了很长一段时间了,它基本上只是使用套接字来发送请求。问题是,当你在游戏服务器上使用该插件时,你必须先在机器上登录SteamCommunity.com来存储cookie。我想将其转换为 C++,以通过首先使用 HTTPS 连接到站点来减轻这一步骤。我已经很长时间没有使用 LibCurl 了,而且我没有太多运气找到设置它所需的信息。

基本上我只是想知道我是否以正确的方式进行此操作,以及我需要使用哪些其他 CURLOPT_ 设置。

void InviteToGroup(const char *pszAuthID)
{
    CURL *curl;
    CURLcode res;

    const char *szCommunityID = GetCommunityID(pszAuthID); // User's Steam Community ID
    const char *szCookie = "76561198018111441%7C%7CC7D70E74A3F592F3E130CCF4CAACD4A7B9CAD993"; // Steam Community Login Cookie
    const char *szInviter = "76561194018311441"; // Inviter's Steam Community ID
    const char *szGroup = "103583791430784257"; // Group Steam Community ID

    const char *request = new char[2048];
    snprintf(request, 2047, "GET /actions/GroupInvite?type=groupInvite&inviter=%s&invitee=%s&group=%s HTTP/1.1\r\nHost: steamcommunity.com\r\nConnection: close\r\nCookie: steamLogin=%s\r\n\r\n", szInviter, szCommunityID, szGroup, szCookie);

    curl = curl_easy_init();
    if(curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.steamcommunity.com");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "myusername:mypass");

        // Attempt to Connect the Steam Community Server
        res = curl_easy_perform(curl);

        // Close the connection
        curl_easy_cleanup(curl);
    }
}

I have a plugin that I have been using for quite some time, and it basically just uses Sockets to send a request. The problem is, when you use the plugin on a game server, you have to login to SteamCommunity.com on the machine first to store the cookie. I want to convert it to C++ to alleviate that step by connecting to the site with HTTPS first. It has been a long time since I have used LibCurl and I am not having too much luck finding the information I need to set this up.

Basically I am just wondering if I am going about this the correct way, and what other CURLOPT_ settings I need to use.

void InviteToGroup(const char *pszAuthID)
{
    CURL *curl;
    CURLcode res;

    const char *szCommunityID = GetCommunityID(pszAuthID); // User's Steam Community ID
    const char *szCookie = "76561198018111441%7C%7CC7D70E74A3F592F3E130CCF4CAACD4A7B9CAD993"; // Steam Community Login Cookie
    const char *szInviter = "76561194018311441"; // Inviter's Steam Community ID
    const char *szGroup = "103583791430784257"; // Group Steam Community ID

    const char *request = new char[2048];
    snprintf(request, 2047, "GET /actions/GroupInvite?type=groupInvite&inviter=%s&invitee=%s&group=%s HTTP/1.1\r\nHost: steamcommunity.com\r\nConnection: close\r\nCookie: steamLogin=%s\r\n\r\n", szInviter, szCommunityID, szGroup, szCookie);

    curl = curl_easy_init();
    if(curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.steamcommunity.com");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "myusername:mypass");

        // Attempt to Connect the Steam Community Server
        res = curl_easy_perform(curl);

        // Close the connection
        curl_easy_cleanup(curl);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆之渊 2024-10-10 11:38:27

您似乎正在发送用户标头,并且可能需要 CURLOPT_HTTPHEADER。您应该使用curl 的slist 来创建它。

如果你想顺便用 C++ 而不是 C 编写,你应该学习正确使用字符串。事实上,您无法写入 const char *,但使用 new 和“希望它足够大”的缓冲区大小并不是您在 C++ 中构造字符串的方式。您可能希望使用 std::ostringstream 创建一个类似的字符串。

You appear to be sending a user header and probably want CURLOPT_HTTPHEADER. You should use curl's slist to create this.

If you want to write in C++ by the way rather than C you should learn to use strings properly. As it is you cannot write into a const char *, but using new with a "hope it's big enough" buffer size is not the way you construct strings in C++. You would probably want to use std::ostringstream to create a string like that one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文