can we pass wstring in CURLOPT_URL - libcurl

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.

Related

How do I create HTTP request with some parameters by POCO?

I'm a new User of POCO and could get HTTP response after HTTP::Request.
By the way, How do I create HTTP request with some parameters? For example, I want to set URI, http://xxxx/index.html?name=hoge&id=fuga&data=foo.
Of course I know it's possible if I set this uri directly. But I want to realize this like below. Does anyone know this way?
URI uri("http://xxx/index.html");
uri.setParam("name", "hoge");
uri.setParam("id", "fuga");
uri.setParam("data", "foo");
If you had looked up the documentation for Poco::URI, you'd see it's done with uri.addQueryParameter("name", "value"):
void addQueryParameter(
const std::string & param,
const std::string & val = ""
);
Adds "param=val" to the query; "param" may not be empty. If val is empty, only '=' is appended to the parameter.
In addition to regular encoding, function also encodes '&' and '=', if found in param or val.
You can also set all the parameters with setQueryParameters.
Unfortunately, Poco doesn't let you set the value of an existing query parameter (or remove it). If you want to do that, you have to clear the query portion of the URI and readd all the parameters you want with their values.

C++ ShellExecute a URL

Tried googling for a few hours, testing different solutions for hours but still just cannot get this to work.
I need a const url base (Ex. http://www.google.com)
Then I need a string input from the user (ex. Mountain Dew) and then combine them.
I've tried making the URL a LPCWSTR, wstring, wchar_t, doing a function to convert them and combine them but I cannot get it to work at all.
std::string baseUrl = "http://www.google.com/";
std::string userAdd;
getline(std::cin, userAdd)
ShellExecute(NULL, TEXT("open"), baseUrl + userAdd, NULL, NULL, SW_SHOWNORMAL);
There's no automatic conversion from std::string to const char*.
Try this: (baseUrl + userAdd).c_str()
and try using ShellExecuteA

URL Variable passing into Curl

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());

unable to print string value using sprintf in c++

i have function that takes two parameters of type LPTSTR, and i am trying print both the values using sprintf like below and i am not able to print the exact values.
int __stdcall Logon(LPTSTR UserName, LPTSTR Password)
{
char Buffer[2048];
sprintf(Buffer,"UserName: %s\n m_Password: %s\n",UserName,Password);
FILE *Ls=fopen("lo.log",a);
fprintf(Ls,Buffer);
fclose(Ls);
}
Either fix "Use Unicode strings" in you project settings or use the
_stprintf(Buffer, _T("UserName: %S\n m_Password: %S\n"), UserName, Password);
and
#include <tchar.h>
If you use Unicode (and you do), use the '%S' format.
Assuming that UserName and Password are stored as wide strings (wchar_t) and you are trying to put them into a char[] buffer, then visual c++ uses %S to accomplish this.
sprintf(Buffer,"UserName: %S\n m_Password: %S\n",UserName,Password);

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!