您的位置:首页 > 编程语言 > Qt开发

QT分析之HTTP请求

2014-02-25 16:08 295 查看
分析QNetworkAccessManager的时候,有一段设定HTTP的请求包的Header,当时没进行深入的分析。

void QHttpNetworkConnectionPrivate::prepareRequest(HttpMessagePair &messagePair)

{

QHttpNetworkRequest &request = messagePair.first;

QHttpNetworkReply *reply = messagePair.second;

// add missing fields for the request

QByteArray value;

// check if Content-Length is provided

QIODevice *data = request.data();

if (data && request.contentLength() == -1) {

if (!data->isSequential())

request.setContentLength(data->size());

else

bufferData(messagePair); // ### or do chunked upload

}

// set the Connection/Proxy-Connection: Keep-Alive headers

#ifndef QT_NO_NETWORKPROXY

if (networkProxy.type() == QNetworkProxy::HttpCachingProxy) {

value = request.headerField("proxy-connection");

if (value.isEmpty())

request.setHeaderField("Proxy-Connection", "Keep-Alive");

} else {

#endif

value = request.headerField("connection");

if (value.isEmpty())

request.setHeaderField("Connection", "Keep-Alive");

#ifndef QT_NO_NETWORKPROXY

}

#endif

// If the request had a accept-encoding set, we better not mess

// with it. If it was not set, we announce that we understand gzip

// and remember this fact in request.d->autoDecompress so that

// we can later decompress the HTTP reply if it has such an

// encoding.

value = request.headerField("accept-encoding");

if (value.isEmpty()) {

#ifndef QT_NO_COMPRESS

request.setHeaderField("Accept-Encoding", "gzip");

request.d->autoDecompress = true;

#else

// if zlib is not available set this to false always

request.d->autoDecompress = false;

#endif

}

// set the User Agent

value = request.headerField("user-agent");

if (value.isEmpty())

request.setHeaderField("User-Agent", "Mozilla/5.0");

// set the host

value = request.headerField("host");

if (value.isEmpty()) {

QByteArray host = QUrl::toAce(hostName);

int port = request.url().port();

if (port != -1) {

host += ':';

host += QByteArray::number(port);

}

request.setHeaderField("Host", host);

}

reply->d_func()->requestIsPrepared = true;

}

如果想模拟IE浏览器,或者想修改成任何你希望的信息,就是在这里修改。

在设定了这些请求信息之后,发送的请求信息包是什么样子的呢?我把工具拦截的信息包具体情况贴出来:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: