#include "httpclient.h"
HttpClient::HttpClient( QWidget *parent, Qt::WindowFlags f )
: QDialog( parent, f )
{
QFont font("ZYSong18030",12);
setFont(font);
setWindowTitle(tr("Http Client"));
urlLabel = new QLabel( this );
urlLabel->setText(tr("Url:"));
urlLineEdit = new QLineEdit( this );
progressBar = new QProgressBar( this );
downloadPushButton = new QPushButton( this);
downloadPushButton->setText( tr( "Download" ) );
cancelPushButton = new QPushButton( this);
cancelPushButton->setText( tr( "Cancel" ) );
exitPushButton = new QPushButton( this);
exitPushButton->setText( tr( "Exit" ) );
QHBoxLayout *hbLayout1 = new QHBoxLayout();
hbLayout1->addWidget( urlLabel );
hbLayout1->addWidget( urlLineEdit );
QHBoxLayout *hbLayout2 = new QHBoxLayout();
hbLayout2->addWidget( progressBar );
QHBoxLayout *hbLayout3 = new QHBoxLayout();
hbLayout3->addWidget( downloadPushButton );
hbLayout3->addWidget( cancelPushButton );
hbLayout3->addWidget( exitPushButton );
QVBoxLayout *vbLayout = new QVBoxLayout( this );
vbLayout->addLayout( hbLayout1 );
vbLayout->addLayout( hbLayout2 );
vbLayout->addLayout( hbLayout3 );
httpClient = new QHttp(this);
connect(httpClient, SIGNAL(requestFinished(int, bool)),this, SLOT(httpRequestFinished(int, bool)));
connect(httpClient, SIGNAL(dataReadProgress(int, int)),this, SLOT(httpDataReadProgress(int, int)));
connect(httpClient, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),this, SLOT(httpResponseHeaderReceived(const QHttpResponseHeader &)));
connect(downloadPushButton,SIGNAL(clicked()),this,SLOT(slotDownload()));
connect(cancelPushButton,SIGNAL(clicked()),this,SLOT(slotCancel()));
connect(exitPushButton,SIGNAL(clicked()),this,SLOT(slotExit()));
cancelPushButton->setEnabled(false);
}
HttpClient::~HttpClient()
{
}
void HttpClient::slotDownload()
{
QUrl url(urlLineEdit->text());
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
if (QFile::exists(fileName))
{
QMessageBox::information(this, tr("Error"),tr("File already exists!"));
return;
}
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Error"),tr("Save file error!"));
delete file;
file = 0;
return;
}
httpClient->setHost(url.host(), url.port() != -1 ? url.port() : 80);
if (!url.userName().isEmpty())
{
httpClient->setUser(url.userName(), url.password());
}
httpRequestAborted = false;
requestId = httpClient->get(url.path(), file);
progressBar->reset();
downloadPushButton->setEnabled(false);
cancelPushButton->setEnabled(true);
}
void HttpClient::slotCancel()
{
httpRequestAborted = true;
httpClient->abort();
downloadPushButton->setEnabled(true);
cancelPushButton->setEnabled(false);
}
void HttpClient::slotExit()
{
accept();
}
void HttpClient::httpRequestFinished(int id, bool error)
{
if (httpRequestAborted)
{
if (file)
{
file->close();
file->remove();
delete file;
file = 0;
}
progressBar->reset();
return;
}
if (id == requestId)
{
progressBar->reset();
file->close();
if (error)
{
file->remove();
QMessageBox::information(this, tr("Error"),tr("Download failed!"));
}
downloadPushButton->setEnabled(true);
cancelPushButton->setEnabled(false);
delete file;
file = 0;
}
}
void HttpClient::httpDataReadProgress(int done, int total)
{
progressBar->setMaximum(total);
progressBar->setValue(done);
}
void HttpClient::httpResponseHeaderReceived(const QHttpResponseHeader &responseHeader)
{
if (responseHeader.statusCode() != 200)
{
QMessageBox::information(this, tr("Error"), tr("Download error!"));
httpRequestAborted = true;
progressBar->reset();
httpClient->abort();
return;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
第8章 网络与通信.rar (84个子文件)
第8章 网络与通信
ftpclient
main.cpp 401B
ftpclient 38KB
ftpclient.h 732B
ftpclient.exe 44KB
ftpclient_zh.qm 841B
ftpclient_zh.ts 2KB
Thumbs.db 13KB
ftpclient.cpp 4KB
Makefile.Release 5KB
ftpclient.pro 392B
Makefile 8KB
networkinformation
main.cpp 455B
networkinformation.cpp 2KB
networkinformation_zh.qm 634B
networkinformation 40KB
networkinformation.pro 421B
Thumbs.db 12KB
Makefile.Release 5KB
networkinformation_zh.ts 2KB
networkinformation.exe 45KB
networkinformation.h 564B
Makefile 8KB
udpbroadcast
server
main.cpp 401B
udpserver_zh.ts 847B
udpserver.pro.user 5KB
udpserver.pro 202B
server 32KB
udpserver.h 497B
server.exe 36KB
Makefile.Release 5KB
udpserver.cpp 1KB
Makefile 8KB
udpserver 32KB
udpserver_zh.qm 529B
client
main.cpp 401B
udpclient 31KB
udpclient.pro.user 5KB
udpclient_zh.qm 477B
Makefile.Release 5KB
udpclient.cpp 1KB
udpclient.exe 35KB
udpclient.h 421B
Makefile 8KB
udpclient_zh.ts 600B
udpclient.pro 202B
Thumbs.db 12KB
chat
server
main.cpp 333B
tcpserver.h 491B
server.h 446B
server.cpp 1KB
tcpclientsocket.h 379B
tcpserver_zh.ts 470B
server 44KB
tcpserver.exe 45KB
tcpserver.cpp 1KB
Makefile.Release 7KB
tcpclientsocket.cpp 651B
tcpserver 44KB
tcpserver.pro 284B
Makefile 9KB
tcpserver_zh.qm 442B
client
main.cpp 334B
tcpclient_zh.qm 639B
tcpclient.pro 202B
tcpclient_zh.ts 2KB
tcpclient.pro.user 5KB
tcpclient.exe 48KB
tcpclient.h 771B
tcpclient 40KB
Makefile.Release 5KB
Makefile 8KB
tcpclient.cpp 4KB
Thumbs.db 12KB
httpclient
main.cpp 338B
httpclient.cpp 4KB
httpclient.exe 43KB
httpclient.h 756B
httpclient.pro 382B
Thumbs.db 12KB
httpclient_zh.qm 623B
httpclient 38KB
Makefile.Release 5KB
httpclient_zh.ts 1KB
Makefile 8KB
共 84 条
- 1
资源评论
DaybreakZhang
- 粉丝: 40
- 资源: 69
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功