How can we parse HTTP response header fields using Qt/C++?

戏子无情 提交于 2019-12-06 05:20:32

The HTTP header should initially be in a QByteArray (because it is in ASCII, not UTF-16), but the method would be the same with a QString:

  • split the header line by line,
  • split each line at the colon character,
  • trim any white spaces (regular spaces and '\r' characters) around the 2 resulting strings before storing them.
QByteArray httpHeaders = ...;
QMap<QByteArray, QByteArray> headers;

// Discard the first line
httpHeaders = httpHeaders.mid(httpHeaders.indexOf('\n') + 1).trimmed();

foreach(QByteArray line, httpHeaders.split('\n')) {
    int colon = line.indexOf(':');
    QByteArray headerName = line.left(colon).trimmed();
    QByteArray headerValue = line.mid(colon + 1).trimmed();

    headers.insertMulti(headerName, headerValue);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!