原创

Qt如何获取外网IP地址

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://shazhenyu.blog.csdn.net/article/details/79307639

直接调用就行:

returnOutIP();这个方法返回的就是外网IP了~


引用头:

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

文章最后发布于: 2018-02-10 23:25:26
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览