Bin grad am verrückt werden. Ich habe eine GUI-Anwendung geschrieben mit welcher ich HTTP Network Requests abschicken kann. Alles funktioniert, auch mit Authentifzierung oder über einen Proxy. Wollte das ganze jetzt noch für die Kommandozeile machen damit man diese Requests auch aus dem Terminal ausführen kann, gleicher Code, jedoch passiert "gar nichts". Weder Fehlermeldung, Noch Meldungungen beim Compilern. Ich verstehe nicht wo das Problem hängt.
Hier der Code der CLI Anwendung:
Code: Alles auswählen
void Requestor::replyFinished(QNetworkReply * reply) {
if (reply->error() == QNetworkReply::NoError) {
cout << QString(reply->readAll()).toStdString() << endl;
}
else {
int code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
cout << reply->errorString().toStdString() << endl;
cout << "Errorcode: ";
cout << code << endl;
cout << QString(reply->readAll()).toStdString().c_str() << endl;
}
cout << "Finished :)" << endl;
}
void Requestor::sslErrorReceived(QNetworkReply*, QList<QSslError> errorlist) {
foreach (QSslError se, errorlist) {
cout << se.errorString().toStdString() << endl;
}
}
void Requestor::authentificationRequired(QNetworkReply* reply, QAuthenticator*) {
cout << "Authentification Required: " << reply->errorString().toStdString() << endl;
}
void Requestor::Request() {
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
this->connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
this->connect(manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
this, SLOT(sslErrorReceived(QNetworkReply*, QList<QSslError>)));
this->connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
this, SLOT(authentificationRequired(QNetworkReply*, QAuthenticator*)));
cout << "use proxy? (y/n):";
string up;
getline(cin, up);
QString useProxy = QString::fromStdString(up);
if (useProxy.toLower() == "y") {
QNetworkProxy proxy;
cout << "Proxy Hostname:";
string input;
getline(cin, input);
QString hostname = QString::fromStdString(input);
cout << "Proxy port:";
int port = cin.get();
proxy.setHostName(hostname);
proxy.setPort(port);
cout << "Proxy user (leave empty for none)";
getline(cin, input);
QString usr = QString::fromStdString(input);
if (usr != "") {
proxy.setUser(usr);
}
cout << "Proxy Password (leave empty for none)";
getline(cin, input);
QString pwd = QString::fromStdString(input);
if (pwd != "") {
proxy.setPassword(pwd);
}
proxy.setType(QNetworkProxy::HttpProxy);
QNetworkProxy::setApplicationProxy(proxy);
manager->setProxy(proxy);
}
cout << "Request URL:";
string input;
getline(cin, input);
QString url = QString::fromStdString(input);
QUrl targetURL(url);
cout << "Request User (leave empty for none):";
getline(cin, input);
QString httpusr = QString::fromStdString(input);
targetURL.setUserName(httpusr);
cout << "Request Password (leave empty for none):";
getline(cin, input);
QString httppwd = QString::fromStdString(input);
targetURL.setPassword(httppwd);
QNetworkRequest netreq(targetURL);
cout << "Waiting until Request finished..." << endl;
//netreq.setHeader(QNetworkRequest::ContentTypeHeader, "text/html");
manager->get(netreq);
}
Code: Alles auswählen
#ifndef REQUESTOR_H
#define REQUESTOR_H
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QSslError>
#include <QList>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkProxy>
#include <iostream>
#include <string>
#include <QString>
using namespace std;
class Requestor : public QObject
{
Q_OBJECT
private slots:
void authentificationRequired(QNetworkReply*, QAuthenticator*);
void sslErrorReceived(QNetworkReply*, QList<QSslError>);
void replyFinished(QNetworkReply *);
public:
Requestor();
void Request();
};
#endif // REQUESTOR_H