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浏览器,或者想修改成任何你希望的信息,就是在这里修改。
在设定了这些请求信息之后,发送的请求信息包是什么样子的呢?我把工具拦截的信息包具体情况贴出来:
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浏览器,或者想修改成任何你希望的信息,就是在这里修改。
在设定了这些请求信息之后,发送的请求信息包是什么样子的呢?我把工具拦截的信息包具体情况贴出来:
相关文章推荐
- QT分析之网络编程(5--8)
- QT分析之网络编程(1--4)
- 【Qt】Windows7 和 Ubuntu11 下安装配置 Qt 5.2.0
- qt中qDebug()无法输出解决办法
- Qt4.8.3移植总结
- QT 4.8 静态库编译方法
- Qt 内存管理机制
- Qt核心剖析: moc
- Qt核心剖析:信息隐藏(3)
- Qt核心剖析:信息隐藏(2)
- Qt核心剖析:信息隐藏(1)
- QT控件之状态栏
- QTP录制不了脚本常见问题解决方法
- Qt中PRO文件写法的详细介绍,很有用,很重要!
- Qt创建和使用动态链接库
- QT工程pro设置实践(with QtCreator)----非弄的像VS一样才顺手?
- qte for arm的编译过程
- QT pro工程文件介绍
- Qt之打包发布(NSIS详解)
- Qt开发:设置QMAKESPEC,生成不同平台的makefile