QT http抓包,post和get方法,解析返回的json包


QT http抓包,post和get方法,解析返回的json包

我用火狐浏览器进行的抓包


代码:

.pro:

QT       += core gui
QT       += webkitwidgets network widgets
QT       += script


.h:
#include

private:
    QNetworkAccessManager *manager;
    QNetworkRequest *req;


cpp:
//以下是http请求的头函数
#include
#include
#include
#include
#include


void Wangpan::on_upload_clicked()
{
   qDebug()<<"xxx";


//post

   QByteArray post_data;
   post_data.append("xxxx");//请求主体
   manager = new QNetworkAccessManager(this);
   connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinish(QNetworkReply*)));
   QNetworkRequest *request = new QNetworkRequest();


       request->setUrl(QUrl("http://xxxx"));//要请求的网页


       request->setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");//qt默认的请求头
       request->setRawHeader("Referer","http://xxxxxxx");//一些必要的请求头
       request->setRawHeader("Cookie","xxxxxxxxx");


       manager->post(*request,post_data);
 
//看看需要什么样的请求头
   //    request->setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
   //    request->setRawHeader("Accept","text/html, application/xhtml+xml, */*");
   //    request->setRawHeader("Referer","http://localhost:8888/login");
   //    request->setRawHeader("Accept-Language","zh-CN");
   //    request->setRawHeader("X-Requested-With","XMLHttpRequest");
   //    request->setRawHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
   //    request->setRawHeader("Content-Type","application/x-www-form-urlencoded");
   //    request->setRawHeader("Accept-Encoding","gzip,deflate");
   //    request->setRawHeader("Host","localhost:8888");
   //    request->setRawHeader("Content-Length","18");
   //    request->setRawHeader("Connection","Keep-Alive");
   //    request->setRawHeader("Cache-Control","no-cache");




     //get
     //manager->get(QNetworkRequest(QUrl("https://xxxx")));
}




void Wangpan::replyFinish(QNetworkReply * reply)
{


    qDebug()<<"对返回信息的JSON包进行解析";
    QString strJsonText = reply->readAll();//这个返回的JSON包所携带的所有信息
    qDebug()<<"所有返回信息"<     QString strText="";
    QScriptValue jsontext;
    QScriptEngine engineText;


    jsontext = engineText.evaluate("value = " + strJsonText );
    QScriptValueIterator iteratorText(jsontext);
    while (iteratorText.hasNext())
    {
           iteratorText.next();
          if(iteratorText.name()=="errmsg")//要查找信息的名字


          {
              qDebug()<<"iteratorText.name:"<               strText = iteratorText.value().toString();
              qDebug()<<"strText"<

              break;
          }
     }


    //reply->deleteLater();






}

你可能感兴趣的:(QT)