QT5 Http/https Access and JSON parsing for practical

Source: Internet
Author: User

Utility QT5 Access http/and HTTPS protocol access

and call JSON parsing

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>Ten#include <QJsonValue> OneMywidget::mywidget (Qwidget *parent): A Qwidget (parent), -UiNewui::mywidget) - { theUI-&GT;SETUPUI ( This); - qurl URL; -M_accessmanager =NewQnetworkaccessmanager ( This); -Url.seturl ("http://ip.taobao.com/service/getIpInfo2.php"); + qsslconfiguration config; - Config.setpeerverifymode (qsslsocket::verifynone); + Config.setprotocol (qssl::tlsv1sslv3); A m_request.setsslconfiguration (config); at m_request.seturl (URL); - Qbytearray SendData; -SendData = qstring::fromstdstring ("ip=27.106.204.0"). Tolocal8bit (); -M_request.setrawheader ("Accept","*/*"); -M_request.setrawheader ("Accept-language","zh-cn,zh;q=0.9"); -M_request.setrawheader ("Content-type","application/x-www-form-urlencoded"); inM_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"); -M_request.setrawheader ("X-requested-with:","XMLHttpRequest"); toM_request.setrawheader ("Referer","http://ip.taobao.com/ipSearch.html"); +M_reply = m_accessmanager->post (m_request,senddata); -  theConnect (m_accessmanager,&qnetworkaccessmanager::finished, This,&mywidget::replyreadfunc); *  $ }Panax Notoginseng  -mywidget::~Mywidget () the { +     DeleteUI; A } the voidMywidget::replyreadfunc (qnetworkreply*Reply) + { -     intNetworkerrors =-1; $Networkerrors = reply->error (); $ QString IP; - QString Country; - QString region; the QString ISP; - Wuyi     if(Networkerrors = =qnetworkreply::noerror) the     { -Qbytearray Databuff = reply->ReadAll (); WuQString result =qstring::fromstdstring (Databuff.tostdstring ()). ToUtf8 (); - qjsondocument document; About qjsonparseerror parseerror; $Document =qjsondocument::fromjson (Result.toutf8 (),&parseerror); -         if(!document.isnull () && Parseerror.error = =qjsonparseerror::noerror) -         { -             if(Document.isobject ()) A             { +Qjsonobject Object = document.Object();//Get Document Object the                 if(!object.isempty ()) -                 { $  the                     if(Object.contains ("Data"))//First Judge if there is the                     { theQjsonvalue dataobj = Object.value ("Data"); the                         if(Dataobj.isobject ())//determine whether an object -                         { inQjsonobject Objectdata =Dataobj.toobject (); the                             if(Objectdata.contains ("IP")) the                             { AboutQjsonvalue Ipval = Objectdata.value ("IP"); the                                 if(Ipval.isstring ()) the                                 { theIP =ipval.tostring (); +                                 } -                             } the                             if(Objectdata.contains (" Region"))Bayi                             { theQjsonvalue Regionval = Objectdata.value (" Region"); the                                 if(Regionval.isstring ()) -                                 { -Region =regionval.tostring (); the                                 } the                             } the                             if(Objectdata.contains ("ISP")) the                             { -Qjsonvalue Ispval = Objectdata.value ("ISP"); the                                 if(Ispval.isstring ()) the                                 { theISP =ispval.tostring ();94                                 } the                             } the                         } the                     }98                 } About             } -country+="Your IP:"+IP;101country+="Your city:"+Region ;102country+="Operator:"+ISP;103 104Qdebug () <<country.tostdstring (). C_STR (); the         }106     }107}

Run results

QT5 Http/https Access and JSON parsing for practical

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.