• QT POST/GET 发送/获取网页数据


    #include <QNetworkReply>
    #include <QNetworkRequest>
    
    QNetworkAccessManager *m_accessManager;
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
        m_accessManager = new QNetworkAccessManager(this);
        QObject::connect(m_accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::finishedSlot(QNetworkReply *reply)
    {
         if (reply->error() == QNetworkReply::NoError)
         {
             QByteArray bytes = reply->readAll();
             qDebug()<<bytes;
             QString string = QString::fromUtf8(bytes);
    
             ui->textBrowser->setText(string.toUtf8());
         }
         else
         {
             qDebug()<<"handle errors here";
             QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
             //statusCodeV是HTTP服务器的相应码,reply->error()是Qt定义的错误码,可以参考QT的文档
             qDebug( "found error ....code: %d %d
    ", statusCodeV.toInt(), (int)reply->error());
             qDebug(qPrintable(reply->errorString()));
         }
         reply->deleteLater();
    }
    
    void Widget::on_pushButton_clicked()
    {
    //POST
        QNetworkRequest *request = new QNetworkRequest();
    
        request->setUrl(QUrl("http://localhost:8888/login"));
    //    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");
    
        QByteArray postData;
        postData.append("myname=lk&mypwd=33");
    
        QNetworkReply* reply = m_accessManager->post(*request,postData);
    }
    
    void Widget::on_pushButton_get_clicked()
    {
    //GET
    
    //    QUrl url("https://kyfw.12306.cn/otn/resources/js/framework/station_name.js");
    //    QUrl url("http://localhost:8888/login");
    //    QUrl url("http://dict.baidu.com/s?wd=name");
    
        QNetworkRequest *request = new QNetworkRequest();
        request->setUrl(QUrl("http://dict.baidu.com/s?wd=name"));
    
        m_accessManager->get(*request);
    }

    http://blog.csdn.net/liukang325/article/details/46915359

  • 相关阅读:
    所谓的日常 #8
    所谓的日常 #7
    所谓的日常 #6
    所谓的日常 #5
    所谓的日常 #4
    所谓的日常 #3
    DFS序+线段树 hihoCoder 1381 Little Y's Tree(树的连通块的直径和)
    Codeforces Round #366 (Div. 2)
    2016 Multi-University Training Contests
    DP套DP HDOJ 4899 Hero meet devil(国王的子民的DNA)
  • 原文地址:https://www.cnblogs.com/findumars/p/6124359.html
Copyright © 2020-2023  润新知