Qt5 http/HTTPS访问 以及JSON解析的实用
实用QT5访问HTTP/以及HTTPS协议访问
并且调用Json解析
1 #include "mywidget.h" 2 #include "ui_mywidget.h" 3 #include <QUrl> 4 #include <QDebug> 5 #include <QByteArray> 6 #include <QSslConfiguration> 7 #include <QJsonDocument> 8 #include <QJsonParseError> 9 #include <QJsonObject> 10 #include <QJsonValue> 11 MyWidget::MyWidget(QWidget *parent) : 12 QWidget(parent), 13 ui(new Ui::MyWidget) 14 { 15 ui->setupUi(this); 16 QUrl url; 17 m_AccessManager = new QNetworkAccessManager(this); 18 url.setUrl("http://ip.taobao.com/service/getIpInfo2.php"); 19 QSslConfiguration config; 20 config.setPeerVerifyMode(QSslSocket::VerifyNone); 21 config.setProtocol(QSsl::TlsV1SslV3); 22 m_Request.setSslConfiguration(config); 23 m_Request.setUrl(url); 24 QByteArray sendData; 25 sendData = QString::fromStdString("ip=27.106.204.0").toLocal8Bit(); 26 m_Request.setRawHeader("Accept","*/*"); 27 m_Request.setRawHeader("Accept-Language","zh-CN,zh;q=0.9"); 28 m_Request.setRawHeader("Content-Type","application/x-www-form-urlencoded"); 29 m_Request.setRawHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"); 30 m_Request.setRawHeader("X-Requested-With:","XMLHttpRequest"); 31 m_Request.setRawHeader("Referer","http://ip.taobao.com/ipSearch.html"); 32 m_Reply = m_AccessManager->post(m_Request,sendData); 33 34 connect(m_AccessManager,&QNetworkAccessManager::finished,this,&MyWidget::ReplyReadFunc); 35 36 } 37 38 MyWidget::~MyWidget() 39 { 40 delete ui; 41 } 42 void MyWidget::ReplyReadFunc(QNetworkReply* Reply) 43 { 44 int NetworkErrors = -1; 45 NetworkErrors = Reply->error(); 46 QString ip; 47 QString country; 48 QString region; 49 QString isp; 50 51 if(NetworkErrors ==QNetworkReply::NoError) 52 { 53 QByteArray databuff = Reply->readAll(); 54 QString result = QString::fromStdString(databuff.toStdString()).toUtf8(); 55 QJsonDocument document; 56 QJsonParseError ParseError; 57 document =QJsonDocument::fromJson(result.toUtf8(),&ParseError); 58 if(!document.isNull() && ParseError.error == QJsonParseError::NoError) 59 { 60 if(document.isObject()) 61 { 62 QJsonObject Object = document.object(); //获取文档对象 63 if(!Object.isEmpty()) 64 { 65 66 if(Object.contains("data")) //先判断有没有 67 { 68 QJsonValue dataObj = Object.value("data"); 69 if(dataObj.isObject()) //判断是否是对象 70 { 71 QJsonObject ObjectData = dataObj.toObject(); 72 if(ObjectData.contains("ip")) 73 { 74 QJsonValue ipVal = ObjectData.value("ip"); 75 if(ipVal.isString()) 76 { 77 ip = ipVal.toString(); 78 } 79 } 80 if(ObjectData.contains("region")) 81 { 82 QJsonValue regionVal = ObjectData.value("region"); 83 if(regionVal.isString()) 84 { 85 region = regionVal.toString(); 86 } 87 } 88 if(ObjectData.contains("isp")) 89 { 90 QJsonValue ispVal = ObjectData.value("isp"); 91 if(ispVal.isString()) 92 { 93 isp = ispVal.toString(); 94 } 95 } 96 } 97 } 98 } 99 } 100 country+="您的ip:"+ip; 101 country+="所在城市:"+region; 102 country+="运营商:"+isp; 103 104 qDebug()<<country.toStdString().c_str(); 105 } 106 } 107 }
运行结果