使用C++中的自定义api通过curl登录失败



我正试图通过curl为我的C++应用程序创建一个登录函数。在成功地将数据传递到服务器之后,服务器应当返回响应代码200。我的信息也和数据库中的信息相同。我正在使用Visual Studio 2019和C++17。我也在Windows上。

注意:我传递给api的链接是正确的,我知道这一点,因为我查看了Visual Studio调试器,并且值是正确的。

我的代码:

std::string API = "http://api.mywebsitenameishere.xyz/ovixau.php?username=" + username + "&password=" + password + "&hwid=TEST&authv=1";
CURL* curl = curl_easy_init();
if (!curl)
printf("Curl failed to init.n");
else
{
int curl_code = curl_easy_perform(curl);
long http_code = 0;
curl_easy_setopt(curl, CURLOPT_URL, API.c_str());
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code == 200 && curl_code != CURLE_ABORTED_BY_CALLBACK)
{
printf("Succesfully logged in!n");
}
else
{
printf("Failed to log in, response code %ld", http_code);
}
}

如果还有什么需要我具体说明的,请随时询问。谢谢

正如Igor所说,我在设置api的url之前发送了一个请求。我需要切换curl_easy_stopt和curl_easy_perform。它现在运行得很好,谢谢你,伊戈尔!

最新更新