QT分析之HTTP请求

简介: 分析QNetworkAccessManager的时候,有一段设定HTTP的请求包的Header,当时没进行深入的分析。void QHttpNetworkConnectionPrivate::prepareRequest(HttpMessagePa...

分析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浏览器,或者想修改成任何你希望的信息,就是在这里修改。
在设定了这些请求信息之后,发送的请求信息包是什么样子的呢?我把工具拦截的信息包具体情况贴出来:
QT分析之HTTP请求 - net_worm - net_worm 的博客

相关文章
|
30天前
|
存储 缓存 安全
第二章 HTTP请求方法、状态码详解与缓存机制解析
第二章 HTTP请求方法、状态码详解与缓存机制解析
|
3天前
|
Web App开发 缓存 JavaScript
使用TypeScript创建高效HTTP代理请求
使用TypeScript创建高效HTTP代理请求
|
30天前
|
JSON Java 数据安全/隐私保护
java中的http请求的封装(GET、POST、form表单、JSON形式、SIGN加密形式)
java中的http请求的封装(GET、POST、form表单、JSON形式、SIGN加密形式)
|
30天前
|
Web App开发 存储 缓存
第八篇 提升网页性能:深入解析HTTP请求优化策略(三)
第八篇 提升网页性能:深入解析HTTP请求优化策略(三)
|
30天前
|
消息中间件 前端开发 JavaScript
第七篇 提升网页性能:深入解析HTTP请求优化策略(二)
第七篇 提升网页性能:深入解析HTTP请求优化策略(二)
|
30天前
|
缓存 自然语言处理 前端开发
第一章 引言-HTTP协议基础概念和前后端分离架构请求交互概述
第一章 引言-HTTP协议基础概念和前后端分离架构请求交互概述
|
1天前
|
JSON 数据格式 Python
Python 的 requests 库是一个强大的 HTTP 客户端库,用于发送各种类型的 HTTP 请求
【6月更文挑战第15天】Python的requests库简化了HTTP请求。安装后,使用`requests.get()`发送GET请求,检查`status_code`为200表示成功。类似地,`requests.post()`用于POST请求,需提供JSON数据和`Content-Type`头。
9 6
|
2天前
|
Python
python做http请求
python做http请求
9 1
|
7天前
|
JSON API 数据格式
Requests库:轻松实现Python中的HTTP请求
Requests是Python的第三方HTTP库,简化了HTTP请求的发送,支持GET、POST等方法。要安装,使用`pip install requests`。Requests以其简洁API和强大功能成为网络编程首选工具,为开发者提供高效稳定的网络交互体验。
20 5
|
8天前
|
API Python
Python HTTP请求库对比,以实战请求豆瓣排行榜为例
对比了Python的几个HTTP请求库,包括`requests`、`http.client`、`aiohttp`、`urllib`、`httpx`、`treq`和`requests-toolbelt`,各有特点和优缺点。选择时应考虑项目需求(如异步支持)、易用性、社区支持、性能和兼容性。示例展示了如何使用`requests`和`aiohttp`库发送豆瓣电影排行榜的GET请求。
8 0

推荐镜像

更多