1
I need to request https and http url. I found several libraries, some easy, some not so easy, but the only one I could install was Curl. I’ve taken several examples, the problem is that in the examples they’re using structured language, and I’m implementing this in qtcreator that uses object orientation. I got the code from the following website: https://gist.github.com/alghanmi/c5d7b761b2c9ab199157
The code went like this Form1.h
#ifndef FORM1_H
#define FORM1_H
#include <QMainWindow>
#include <iostream>
#include <string>
#include <curl/curl.h>
using namespace std;
namespace Ui {
class Form1;
}
class Form1 : public QMainWindow
{
Q_OBJECT
public:
explicit Form1(QWidget *parent = 0);
string readBuffer;
CURL *curl;
CURLcode res;
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
~Form1();
private slots:
void on_btnLogin_clicked();
private:
Ui::Form1 *ui;
};
#endif // FORM1_H
Form1.cpp
#include "form1.h"
#include "ui_form1.h"
#include <QWebView>
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
#include <string>
using namespace std;
Form1::Form1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Form1)
{
ui->setupUi(this);
this->curl = curl_easy_init();
}
Form1::~Form1()
{
delete ui;
}
void Form1::on_btnLogin_clicked()
{
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);
cout << readBuffer << std::endl;
}
The error is as follows
form1.cpp:35: error: invalid use of member function (did you forget the '()' ?)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);`
What
curl_easy_setopt
expects to receive as argument? It seems that did not recognize thatWriteCallBack
as a function, but as a method (Member Function? That’s what I’ve interpreted, method, member role)– Jefferson Quesado
Qt-5 has libraries capable of making an HTTP request, check it out: https://karanbalkar.com/2014/02/sending-a-http-request-using-qt-5-framework/
– Lacobus
Lacobus, I’ve read the code a little bit about him, but look, I’m a beginner in c++ and Qt5, I was programming in python2.7 + Qt4, and to me this library is still too complex to use
– Mega Anim
I found this library here https://github.com/whoshuu/cpr It seems to be based on Curl + python request, and it’s very similar to what I’m used to using, but I don’t know how to install it, it would be great to use it
– Mega Anim