Qt使用QNetworkReply获取外部IP地址

时间:2017-12-16 10:00:51

标签: c++ json qt qnetworkaccessmanager qnetworkreply

美好的一天

说明:

我的应用程序需要获取外部IP地址并将其与内部获取的地址匹配,从而允许应用程序继续运行。

为此,我正在使用QNetworkAccessManagerQNetworkReply来实现此目的。

我的代码是使用this示例作为参考构建的。

我尝试了什么:

通过从ipify API获取JSon对象,可以获取外部IP。

我通过以下方式确认了这一点:

curl "https://api.ipify.org?format=json"

反过来以以下格式响应我当前的IP地址:

{"ip":"255.255.255.255"}

这是一个JSonObject。使用这个,我创建了下面的代码。

问题:

问题很简单,我没有回应。 post请求已执行,但根本没有响应(或finished)信号被触发。

  • POST - > GET请求

我已经更改了get请求的代码,因为这解决了this thread上的无响应问题。

我是通过在URL中使用查询参数指定整个网址来完成此操作的:

QNetworkRequest request(QUrl("https://api.ipify.org?format=json"));

包括标题内容类型和大小(如下例所示,最后使用:{/ p>调用QNetworkAccessManager::get()

replyExternalAddress = networkManager->get(request);

但是这也没有回应。

我认为这是我想念的小事,但我根本看不到它。

么?

查询外部IP的代码:

// public callable method, starting network request
void APICommunicator::requestExternalAddress(){
    qInfo(apicommunicator) << "Requesting external IP address from ipify.org";

    // creates network request
    // specifies "format=json"
    QUrlQuery postData;
    postData.addQueryItem("format", "json");
    QByteArray encodedQuery = postData.toString(QUrl::FullyEncoded).toUtf8();
    QNetworkRequest request(QUrl("https://api.ipify.org"));

    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(encodedQuery.size()));

    // creates merged URL from URL and query items and sends a post:
    //    https://api.ipify.org?format=json
    replyExternalAddress = networkManager->post(request, encodedQuery);

    // Creates QMetaObject::Connection connection for finished signal from QNetworkReply
    conExternalAddress = QObject::connect(replyExternalAddress, SIGNAL(finished()), this, SLOT(externalAddressResponse()));

    // attach error listener to reply
    addErrorListener(replyExternalAddress, conExternalAddress);


}

void APICommunicator::externalAddressResponse(){
    qDebug(apicommunicator) << "External Address response recieved";

    // disconnect signals
    QObject::disconnect(conExternalAddress);
    QObject::disconnect(conErrorListener);

    // read all output from JSon object
    QByteArray ba = replyExternalAddress->readAll();

    // delete QNetworkReply
    replyExternalAddress->deleteLater();

    LogMessageHandler::writeToApiLog(QString("\n\nCALL EXTERNAL [" + replyExternalAddress->request().url().toString() + "]\n" + QString(ba)));

    QJsonObject doc = QJsonDocument::fromJson(ba).object();
    QString ip = doc.value("ip").toString();    
    QHostAddress address = QHostAddress();

    if (ip.isEmpty()) {
        qWarning(apicommunicator) << "External Address: no data received";
    }
    else {
        address = QHostAddress(version);
    }

    // replies with address to external slot (in main application)
    emit ExternalAddressReply(address);
}

1 个答案:

答案 0 :(得分:1)

问题是您要发送POST个请求,而ipify.org只需要GET个请求。您似乎有误解,您需要发送POST请求才能发送参数(format=json)以及您的请求,但事实并非如此。在您的代码中,您将参数作为POST数据发送,这与您在浏览器中提交Web表单时使用的技术相同(因为您要将内容类型标题设置为application/x-www-form-urlencoded

您绝对不需要模仿发送表单的Web浏览器的请求,以便能够与API ipify.org提供的API进行通信。 ipify.org提供了一个更简单的界面;您只需要在get请求中发送查询字符串。通过提供提供构建url查询的方法的类QUrlQuery,Qt使工作变得更加容易。这是一个有效的例子:

#include <QtCore>
#include <QtNetwork>

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    QNetworkAccessManager networkManager;

    QUrl url("https://api.ipify.org");
    //the query used to add the parameter "format=json" to the request
    QUrlQuery query;
    query.addQueryItem("format", "json");
    //set the query on the url
    url.setQuery(query);

    //make a *get* request using the above url
    QNetworkReply* reply = networkManager.get(QNetworkRequest(url));

    QObject::connect(reply, &QNetworkReply::finished,
                     [&](){
        if(reply->error() != QNetworkReply::NoError) {
            //failure
            qDebug() << "error: " << reply->error();
        } else { //success
            //parse the json reply to extract the IP address
            QJsonObject jsonObject= QJsonDocument::fromJson(reply->readAll()).object();
            QHostAddress ip(jsonObject["ip"].toString());
            //do whatever you want with the ip
            qDebug() << "external ip: " << ip;
        }
        //delete reply later to prevent memory leak
        reply->deleteLater();
        a.quit();
    });
    return a.exec(); 
}