直接调用就行:
.returnOutIP();这个方法返回的就是外网IP了~
标签:responseData,return,Qt,TcpSever,获取,manager,QString,IP地址,include From: https://blog.51cto.com/u_15854865/5984203
引用头:
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QEventLoop>
调用:
QString TcpSever::returnOutIP()
{
return GetNetIP(GetHtml("http://2017.ip138.com/ic.asp"));
}
私有:
QString TcpSever::GetHtml(QString url)
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
QByteArray responseData;
QEventLoop eventLoop;
QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
eventLoop.exec();
responseData = reply->readAll();
return QString(responseData);
}
QString TcpSever::GetNetIP(QString webCode)
{
QString tmp = webCode.split("[").last();
return tmp.split("]").first();
}