URL Variable passing into Curl - c++

I'm new to cURL and needed it for my assignment. And I'm using C++ for this.
I have this particular line which works fine.
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
But my problem occurs when I modify the URL into variable. i.e
string URL = "http://www.google.com";
curl_easy_setopt(curl, CURLOPT_URL, URL);
My program crashes. Anyone can point to me what's my mistakes?

CURLOPT_URL: Pass in a pointer to the actual URL to deal with. The parameter should be a char * to a zero terminated string...
If you hold the URL in a std::string variable you should use std::string::c_str().
std::string URL = "http://www.google.com";
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());

Related

How can I download a file in c++ with parameters not using libcurl

so I have a question on how to download a file off my site using parameters in c++ example: "test.com/test.php?user=name&token=sgdashg"
I can't seem to figure out how I tried UrlDownloadFileA just got the main path without the parameters.
URLDownloadToFile(NULL, "localhost:8080/test.php?", "username=" + UserNameBuffer, "&token=" TokentBuf, "C:\\", 0, NULL);
You need to build the full URL as a string before you pass it to the second argument of the function.
So something like this:
std::string url = "localhost:8080/test.php?username=";
url = url + UserNameBuffer + "&token=" + TokentBuf;
URLDownloadToFile(NULL, url.c_str() , "C:\\test.txt", 0, NULL);
The concept of HTTP parameters has nothing to do with the concept of function parameters for the URLDownloadToFile function.

can we pass wstring in CURLOPT_URL

I tried passing wstring url but it is failing,
is there any correct way to pass it or
can we pass wstring encoded url in CURLcode curl_easy_setopt(CURL *handle, CURLOPT_URL, ?)
No, you can't. CURLOPT_URL takes a C string as input.

How to retrieve table information using libcurl

I am new to using the libcurl libraries in C++ just to learn some new stuff, however i cant seem to find to much useful info on the subject with good practical examples.
I am trying to retrieve some stats from this website:
http://www.squawka.com/football-player-rankings#performance-score#player-stats#spanish-la-liga|season-2014/2015#all-teams#all-player-positions#16#39#0#0#90#23/08/2014#28/12/2014#season#1#all-matches#total#desc#total
For this, after the proper includes i am pulling the webpage using:
int main()
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
// Set URL
curl_easy_setopt(curl, CURLOPT_URL, "http://www.squawka.com/football-player-rankings#performance-score#player-stats#spanish-la-liga|season-20
14/2015#r-madrid#all-player-positions#16#34#0#0#90#23/08/2014#14/11/2014#season#1#all-matches#total#desc#total");
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
// Check for errors
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
// Print the code
cout << res << endl;
// Always cleanup each call to curl_easy_init
curl_easy_cleanup(curl);
}
return 0;
}
From the websites source code i can check for example that the name Messi is in:
<div class="stats-player-name">Messi</div> in <tr style class="ranking-data-row" data-id="1569" data-row="1">.
However if i run a search on the printed code i canĀ“t see the name Messi, nor his stats anywhere. What am i missing so i can tell the site to download all the players data? Shouldnt this be done automatically when i give the URL?
I tried using something like this: Add paramethers to libcurl GET in c++ with no success.
Thanks in advance for some basic guidelines so i can continue with this.

WebApplication to Delete file in Sharepoint

I am trying to delete a file in Sharepoint from my ASP.NET web application.
For which i have added List.asmx web reference which has Delete attachment method. This method has to be passed with three parameters.
DeleteAttachment(String ListName,String ListItemID, String url);
If my file location is below
http://example.com/sites/xxx/xxx/xxx/Shared Documents/yyy/zzz/Review comments_docx.doc
What would be the ListName, ListItemID, url.
Below is my code. Can anyone suggest also correct if i am doing anything wrong.
wsLists.Lists objList = new wsLists.Lists();
objList.Credentials = new NetworkCredential(GlobalVariablesBO.UserID, GlobalVariablesBO.Password, GlobalVariablesBO.Domain);
objList.Url = string.Concat("http://example.com/sites/xxx/xxx/xxx/_vti_bin/lists.asmx");
string url = Convert.ToString(item.GetDataKeyValue("SharePointURL"));
objList.DeleteAttachment("Shared Documents", "3", url);
UpdateListItems(String ListName, XMLNode updates)
is a actual method to delete a document in sharepoint.

C++ - how to send a HTTP post request using Curlpp or libcurl

I would like to send an http post request in c++. It seems like libcurl (Curlpp) is the way to go.
Now, here is a typical request that am sending
http://abc.com:3456/handler1/start?<name-Value pairs>
The name values pairs will have:
field1: ABC
field2: b, c, d, e, f
field3: XYZ
etc.
Now, I would like to know how to achieve the same using curlpp or libcurl.
Code snippets will really help.
Don't have experience with Curlpp but this is how I did it with libcurl.
You can set your target url using
curl_easy_setopt(m_CurlPtr, CURLOPT_URL, "http://urlhere.com/");
POST values are stored in a linked list -- you should have two variables to hold the begin and the end of that list so that cURL can add a value to it.
struct curl_httppost* beginPostList;
struct curl_httppost* endPostList;
You can then add this post variable using
curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
Submitting then works like this
curl_easy_setopt(m_CurlPtr, CURLOPT_POST, true);
curl_easy_setopt(m_CurlPtr, CURLOPT_HTTPPOST, beginPostList);
curl_easy_perform(m_CurlPtr);
Hope this helps!