Created
May 5, 2014 20:12
-
-
Save alghanmi/c5d7b761b2c9ab199157 to your computer and use it in GitHub Desktop.
cURL C++ Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <curl/curl.h> | |
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) | |
{ | |
((std::string*)userp)->append((char*)contents, size * nmemb); | |
return size * nmemb; | |
} | |
int main(void) | |
{ | |
CURL *curl; | |
CURLcode res; | |
std::string readBuffer; | |
curl = curl_easy_init(); | |
if(curl) { | |
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com"); | |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); | |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); | |
res = curl_easy_perform(curl); | |
curl_easy_cleanup(curl); | |
std::cout << readBuffer << std::endl; | |
} | |
return 0; | |
} |
- It looks like you did not include the
-lcurl
library when you compiled, hence the "unresolved external symbol" errors. https://example.com
does not support--http3
, yet; you could test this using any HTTP/3 verifier.
Try using any of the HTTP/3 supported websites, some are mentioned here.- I compiled and executed your code using
https://quic.tech
but received acurl_easy_perform() failed: SSL connect error
error.
Most probably because I don't have the OpenSSL libraries installed, and this is expected.
But the code itself is executing fine.
#include <curl/curl.h>
int main()
{
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "https://quic.tech");
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_3);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
}
Hope that helps :)
I developed a client which is receiving data from server occasionally. But when I receive first data, then program exists. I tried while loop like windows message loop, but it gives me last data.
How can I stay in program so that I can receive more messages from server.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
guys i dont know how to integrate my project can you halp me ?
Build started...
1>------ Build started: Project: cmk, Configuration: Release Win32 ------
1>cmk.cpp
1>cmk.obj : error LNK2001: unresolved external symbol _curl_easy_perform
1>cmk.obj : error LNK2001: unresolved external symbol _curl_easy_init
1>cmk.obj : error LNK2001: unresolved external symbol _curl_easy_strerror
1>cmk.obj : error LNK2001: unresolved external symbol _curl_easy_cleanup
1>cmk.obj : error LNK2001: unresolved external symbol _curl_easy_setopt
1>C:\Users\Mustafa_Owner\source\repos\cmk\Release\cmk.exe : fatal error LNK1120: 5 unresolved externals
1>Done building project "cmk.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Elapsed 00:01,275 ==========
main.cpp - > \n
#include
#include <curl/curl.h>
int main()
{
CURL* curl;
CURLcode res;
}