https://img-blog.csdnimg.cn/32db9ce43ef64316a2e37a31f4cee033.gif
编程小鱼酱yu612.com,点击前往

Qt之小项目实战----刷csdn访问量工具

一、设计

使用QT5.8 +QT creater

使用这两个累

      QNetworkReply

      QNetworkAccessManager

       1.定义访问的文章

       2.模拟浏览器发送请求


二、核心编码


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>


#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QTimer>
#include <QSslConfiguration>
#include <QString>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();



public slots:
    void slot_requestFinished();
    void on_pushButton_clicked();
    void slot_requestTimeout();

signals:
    void signal_requestFinished(bool bSuccess,const QString& strResult); //http请求结束

private:
    Ui::MainWindow *ui;
    QNetworkAccessManager* m_pNetworkManager;//网络管理类
    QNetworkReply* m_pNetworkReply; //封装请求返回信息
    QTimer* m_pTimer; //请求超时计时器
    QString m_strUrl; //记录当前请求的url
};

#endif // MAINWINDOW_H




 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_pNetworkReply = NULL;
    m_pTimer = new QTimer(this);
    connect(m_pTimer,SIGNAL(timeout()),this,SLOT(slot_requestTimeout()));//超时信号

}

MainWindow::~MainWindow()
{
    delete ui;
}
const int nHTTP_TIME = 3000; //10秒

void MainWindow::on_pushButton_clicked()
{
    m_pTimer->start(3000);

    m_pNetworkManager = new QNetworkAccessManager(this);

    QString strUrl = "http://blog.csdn.net/hiwoshixiaoyu/article/details/72763130";
    QNetworkRequest netRequest;
    netRequest.setRawHeader("Accept","*/*");
    netRequest.setRawHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
    netRequest.setUrl(QUrl(strUrl)); //地址信息
    if(strUrl.toLower().startsWith("https"))//libeay32.dll和ssleay32.dll
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        netRequest.setSslConfiguration(sslConfig);
    }
    m_pNetworkManager->clearAccessCache();
    m_pNetworkReply = m_pNetworkManager->get(netRequest); //发起get请求
    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);

}
#include <windows.h>

void MainWindow::slot_requestFinished()
{
    m_pTimer->stop();//关闭定时器
    QByteArray resultContent = m_pNetworkReply->readAll();
    QTextCodec* pCodec = QTextCodec::codecForName("UTF-8");
    QString strResult = pCodec->toUnicode(resultContent);
    int nHttpCode = m_pNetworkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();//http返回码

    QString qStrDest1 = "访问:";
    QString qStrDest2 = "</span>";
    if(nHttpCode == 200)//成功
    {
        int pos1 = strResult.indexOf(qStrDest1);
        QString temp = strResult.mid(pos1,200);

        int pos2 = temp.indexOf(qStrDest2)+qStrDest2.length();
        QString temp2 = temp.left(pos2);
        ui->m_RequestNum->setText(temp2);

        emit signal_requestFinished(true,strResult);//请求失败
    }
    else
    {
        emit signal_requestFinished(false,strResult);//请求失败
    }
    m_pNetworkReply->deleteLater();

    Sleep(1000);
    on_pushButton_clicked();

}

//请求超时
void MainWindow::slot_requestTimeout()
{
    emit signal_requestFinished(false,"timeout");//请求失败
    m_pNetworkReply->deleteLater();

}




三、总结


  1.工具源码下载地址。

     http://download.csdn.net/detail/hiwoshixiaoyu/9857560

  2.后续推出一个起线程的版本,防止总卡死的问题。

 


posted @ 2017-06-01 14:18  鱼酱  阅读(415)  评论(0编辑  收藏  举报

https://img-blog.csdnimg.cn/32db9ce43ef64316a2e37a31f4cee033.gif
编程小鱼酱yu612.com,点击前往