开发者

C++ LibCurl to Send HTTPS Request

开发者 https://www.devze.com 2023-01-26 19:06 出处:网络
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 l开发者_JAVA技巧

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 l开发者_JAVA技巧ogin 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);
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消