recently managed to use a libcurl in a test program for downloading a files.
The code is this:
CURL * curl;
FILE * fout;
CURLcode result;
char * url = "http://blablabla.com/blablabla.txt";
char filename[FILENAME_MAX] = "blablabla.txt";
curl = curl_easy_init();
if (curl)
{
fout = fopen(filename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fout);
result = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fout);
}
and those things for directives:
#define CURL_STATICLIB
#include <curl/curl.h>
My question is how to make that I don't need to copy all of its dlls in the same dir with the exec to make it work:
libcurl.dll
libeay32.dll
libidn-11.dll
librtmp.dll
libssh2.dll
libssl32.dll
zlib1.dll
Cannot find info about that in the homesite (http://curl.haxx.se) of the library :|
You mean, "how do I statically link libcurl"?
5.7 in the FAQ says:
When building an application that uses the static libcurl library, you must add -DCURL_STATICLIB to your CFLAGS. Otherwise the linker will look for dynamic import symbols. If you're using Visual Studio, you need to instead add CURL_STATICLIB in the "Preprocessor Definitions" section.
CURL *curl;
CURLcode res;
CString strUser;
FILE *ftplister;
m_FTPUserName.GetWindowTextW (strUser);
CT2CA PUF_File_HostName(PUF_FIle_Host);
CT2CA FTP_UserName(FTP_USERNAME);
FTP_PASSWORD.Append(L":");
FTP_PASSWORD.Append(FTP_USERNAME);
CT2CA FTP_Password(FTP_PASSWORD);
CT2CA FTP_AddressName(FTP_Address);
CString strLocalFile;
CString strFileName;
m_FTPFileName.GetWindowTextW(strFileName);
strLocalFile.Append(FTP_FileName);
strLocalFile.Append(L"\\");
strLocalFile.Append(strFileName);
CT2CA PUF_LocalFile(strLocalFile);
//ofstream stream2(PUF_LocalFile);
curl = curl_easy_init();
if(curl)
{
ftplister=fopen(PUF_LocalFile,"wb");
curl_easy_setopt(curl,CURLOPT_URL,PUF_File_HostName);
if(strUser!=_T(""))
{
curl_easy_setopt(curl,CURLOPT_USERPWD,FTP_Password);
}
curl_easy_setopt(curl, CURLOPT_FTP_SSL, TRUE);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftplister);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(ftplister);
}
Related
This question already has an answer here:
libcurl returns CURLcode 23
(1 answer)
Closed 7 months ago.
I am trying to get the idea about libcurl and I am trying to download simple photo from the url. But my program crashes when it goes inside curl_easy_perform() API. Any idea about it?
#include <stdio.h>
#include <curl/curl.h>
#include <QDebug>
#include <string>
int main(void)
{
CURL *curl;
FILE *fp;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
std::string url = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Nusfjord_road%2C_2010_09.jpg/1280px-Nusfjord_road%2C_2010_09.jpg";
//std::string url = "https://ra-jenkins-nyk01.siemens.net/job/TestCase_Logs/ws/OBU_Int_Build/TestCases/TC_30520_OBU_detects_dir_to_increase_whn_conf_increase_dir/TC_30520_OBU_detects_dir_to_increase_whn_conf_increase_dir.ctr";
char outfilename[FILENAME_MAX] = "D:/ankit.jpg";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
//curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
if(res == CURLE_OK)
printf("Download Successful.");
else
printf("Not sucessful");
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
Compiler :- mingw
libcurl version :7.84.0
I wonder if you are using libcurl library as a win32 dll or static library, according to the libcurl official documentation, it says
If you are using libcurl as a win32 DLL, you MUST use a
CURLOPT_WRITEFUNCTION if you set this option or you will experience
crashes.
You may want to see in here: click
How can i download a file from a webserver, and save it in a specific path on Linux?
I have used this code (this is an expample):
CURL *curl;
FILE *fp;
CURLcode res;
const char *url = "http://google.com";
char outfilename[FILENAME_MAX] = "\\home\\user_name\\";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
But it doesn't work perfectly, because it save the file with the absolute path in his name only in the working directory!
Can anyone help me with this problem?
Thank you for your attention!
you need to use a forward slash, I suppose
char outfilename[FILENAME_MAX] = "/home/user_name/";
does anyone knows the exact syntax for using elasticsearch with libcurl?
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:9200/bro-201409170900/http/ZinAvJ-ETT-mycy2jyRkdg/_update -d");
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"script\" : \"ctx._source.longitude += 3\"}");
curl_easy_perform(curl);
curl_easy_cleanup(curl);
return 1;
}
This code does ont update the longitude parameter and I don't know why.
One should not specify the "-d' in the url. The command line tool is just build on top of libcurl.
If you want to see what the c code for the post request would look like you could use the libcurl option with command line .
example :
curl localhost:9200/bro-201409170900/http/ZinAvJ-ETT-mycy2jyRkdg/… -d '{ "script" : "ctx._source.longitude += 2"}' --libcurl output.c
A simple "C" implementation would look something on these lines
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
char *postFields = "{\"script\" : \"ctx._source.longitude += 3\"}";
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:9200/bro-201409170900/http/ZinAvJ-ETT-mycy2jyRkdg/_update");
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,postFields);
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,strlen(postFields));
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
I get this error from Curl, yet, the site is available, and I see nothing going out on wireshark.
What might cause this error ?
I've try to run it against www.google.com and got the same error.
this very code was working a few hours ago. I have no idea what might cause this.
here is the code:
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "Content-Type: text/xml");
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
struct rcvdstring s;
init_string(&s);
string FullAddress = URL+Method;
curl_easy_setopt(curl, CURLOPT_URL, FullAddress.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, DATA.c_str());
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
char buf[1024];
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
Respons.assign(s.ptr);
return res;
}
* Update - took the exe to another computer, it works there. This is local to my computer....
Ok, Moving the code to another computer - showed me the code is OK.
The problem was with Norton Internet Security that after a few days of developing decided to block my communication DLL.
So I have a C++ application that takes a value from a key in a settings.INI file, uses libcurl to reach out to a PHP page, post that data, and it should return some different data.
And it actually works, aside from grabbing the data from the INI file. If I explicitely type in for the POSTFIELDS option for libcurl (e.i.: "Serial=454534" , instead of the variable storing the data that it retrived from my file).
Here's some code..
PHP:
<?
include("DBHeader.inc.php");
$Serial= $_POST['Serial'];
$sql = "SELECT * FROM `LockCodes` WHERE `sLockCode`=\"$Serial\"";
$RS=mysql_query($sql, $SQLConn);
$num_rows=mysql_num_rows($RS);
if ($num_rows>0)
{
while ($row = mysql_fetch_array($RS))
{
echo $row['iLockCodeID'];
}
}
else
{
echo "...";
}
?>
Snippet of C++ code:
TCHAR szKeyValue[36];
GetPrivateProfileString(_T("Test"), _T("LockCode"), _T(""), szKeyValue, 36, _T("C:\\Test\\Settings.INI"));
CString sLockCode = szKeyValue;
CURL *curl;
CURLcode res;
CString Serial = _T("Serial=") + sLockCode;
string LCID;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://regserver2.nyksys.com/GetLCID.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Serial);
res = curl_easy_perform(curl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if (data == _T("..."))
{
data = "0";
AfxMessageBox("Invalid Serial Number");
exit(0);
}
My Settings.INI is in the standard format..
[Test]
LockCode=1D4553C7E7228E462DBAAE267977B7CDED8A
What happens is whenever I use the variable "Serial" instead of typing it in, the PHP page returns "..." instead of the desired result.
I feel like I'm missing something obvious. Any help would be TREMENDOUSLY appreciated.
I am guessing that you have _UNICODE defined, which means that TCHAR is wchar_t, CString is CStringT<wchar_t>, and the code:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Serial);
actually passes a wide char string to curl_easy_setopt() when the function is expecting a narrow char string. If your machine is little Endian, then curl_easy_setopt() interprets the parameter as the string "S\x00e\x00r\x00i\x00... (on a Big Endian machine, it's "\x00S\x00e\x00r\x00i...) and because curl_easy_setopt() will use strlen() when CURLOPT_POSTFIELDSIZE is not set, the entire POST request body on a little Endian machine is S. See, for example, http://codepad.org/JE2MYZfU
What you need to do is use narrow char strings:
#define ARRAY_LEN(arr_id) ((sizeof (arr_id))/(sizeof ((arr_id)[0])))
char szKeyValue[36];
GetPrivateProfileStringA("Test", "LockCode", "", szKeyValue, ARRAY_LEN(szKeyValue), "C:\\Test\\Settings.INI");
CURL *curl;
CURLcode res;
CStringT<char> Body = CStringT<char>("Serial=") + szKeyValue;
string LCID;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://regserver2.nyksys.com/GetLCID.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Body);
res = curl_easy_perform(curl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
//...
Also, your PHP script looks vulnerable to SQL injection.
EDIT: One more thing. Are you setting CURLOPT_POST to 1?
curl_easy_setopt(curl, CURLOPT_POST, 1);