请问如何做一个tcp客户端访问网页
本帖最后由 oXiFangShiBai 于 2013-02-01 10:28:15 编辑 使用tcp协议,来访问网页,这样主要是做一个长连接
我的实现思路是:
1.连接 服务器 【举例用百度 www.baidu.com】
2.发送http协议头,获取服务器返回网页内容 并 处于监控状态,因为服务器会每隔1分钟发心跳包给我,我的目的就是要得到心跳包的内容,
3.我的代码如下,出现的问题是,发送了http协议头,服务器没有任何反映,我用wireshark抓包,甚至没有变成http请求,我想,一定是我发送的时候出了问题,但自己研究了半天没弄好,所以来请教大家
下面贴上我的代码
client.h
//client.h
#ifndef TRYCLI_H_
#define TRYCLI_H_
#include <QtNetwork>
#include <QtGui>
#include <QtCore>
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
class Client : public QWidget
{
Q_OBJECT
private:
bool isConnected;
QLineEdit *serverIpEdit;
QLabel *label;
QPushButton *startButton;
QTcpSocket *tcpClient;
quint16 blockSize;
QString sendString;
QString readString;
public:
Client();
~Client();
public slots:
void displayError(QAbstractSocket::SocketError socketError);
void newConnect();
void readMessage();
void sendMessage();
};
#endif
//client.cpp
#include "client.h"
#include <QMessageBox>
#include <QHBoxLayout>
#include <QtEvents>
#include <QTextEdit>
#include <QDebug>
#include <QString>
Client::Client()
{
setWindowTitle("Client");
resize(300, 100);
serverIpEdit = new QLineEdit("www.baidu.com");
startButton = new QPushButton("start");
label = new QLabel("Emtpy");
isConnected = false;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(serverIpEdit);
layout->addWidget(label);
layout->addWidget(startButton);
setLayout(layout);
tcpClient = new QTcpSocket(this);
connect(startButton, SIGNAL(clicked()), this, SLOT(newConnect()));
connect(tcpClient, SIGNAL(connected()), this, SLOT(sendMessage()));
connect(tcpClient, SIGNAL(readyRead()), this, SLOT(readMessage()));
connect(tcpClient, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}
Client::~Client()
{
}
void Client::newConnect()
{
blockSize = 0;
tcpClient->abort();
tcpClient->connectToHost(serverIpEdit->text(), 80);
}
void Client::sendMessage()
{
qDebug()<<"runing:sendMessage";
char* ch="GET / HTTP/1.1 \n Connection: Keep-Alive";
QByteArray ba = sendString.toLatin1();
ch=ba.data();
tcpClient->write(ch,39);
qDebug()<<ch;
}
void Client::readMessage()
{
qDebug()<<"runing:readMessage";
QDataStream in(tcpClient);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0)
{
if (tcpClient->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpClient->bytesAvailable() < blockSize)
return;
in >> readString;
label->setText(readString);
qDebug()<<tcpClient->readAll();
}
void Client::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Fortune Client"),
tr("The following error occurred: %1.")
.arg(tcpClient->errorString()));
}
}
//main.cpp
#include "client.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Client w;
w.show();
return a.exec();
}