Qt QNetworkAccessManager or other method get html status code without getting page contenet?

StackOverflow https://stackoverflow.com/questions/13874616

  •  07-12-2021
  •  | 
  •  

Вопрос

i need to get web sites html status codes today i just do simple get request to the domain , and then i get the status code as part of the response , but also the site index.html content .

pNetworkManager = new QNetworkAccessManager(this);
reply = pNetworkManager->get(request);
QVariant vStatusCodeV =  reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);


data=reply->readAll();

this last function i like to avoid if it can be avoided , is there any way to get only the domain status code ?

Это было полезно?

Решение 2

I agree with @shevron's answer, but if the site you're communicating with isn't "clever" enough to implement the HEAD request, you can still avoid the readAll() call.

QByteArray line = reply->readLine();  //< eg "HTTP/1.0 200 OK"
QList<QByteArray> chunks = line.split(' ');
QString statusCode = chunks[1];

That should avoid the memory overhead of readAll().

Другие советы

Maybe you can send a HEAD request instead of a GET request?

This is not a Qt / client specific solution, but is the approach recommended by the HTTP protocol when you don't need the content, but just want to get the headers that a request would normally produce, for example in order to validate that the page exists.

I suppose this could be done with QNetworkAccessManager using the head() method

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top