Qt之小项目实战----刷csdn访问量工具
【摘要】
一、设计
使用QT5.8 +QT creater
使用这两个累
QNetworkReply
QNetworkAccessManager
&...
一、设计
使用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.后续推出一个起线程的版本,防止总卡死的问题。
文章来源: yujiang.blog.csdn.net,作者:鱼酱2333,版权归原作者所有,如需转载,请联系作者。
原文链接:yujiang.blog.csdn.net/article/details/72830694
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)