解决QNetworkReplyImplPrivate::error: Internal problem, this method must only be called once.错误

在QNetworkReply *m_reply使用http下载文件时,如果此时的m_reply已经处于错误状态,则不要调用 m_reply->abort();
或者 m_reply->close();

  QNetworkAccessManager *manager = new QNetworkAccessManager(this);
  manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
  m_reply = m_networkManager.get(QNetworkRequest(requestedUrl));
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), 
		this, SLOT(httpError(QNetworkReply::NetworkError)));

可以获得几乎所有Qt自定义的http错误(也可以自己增加新的错误码)
这样做也没用:

 if (m_reply->isRunning())
{
     
	m_reply->abort();  //也不要m_reply->close(); 
}

http处于错误时,m_reply->isRunning()还返回true,Qt文档写得不太清楚。此时再调用m_reply->abort(); 或者m_reply->close();
都会报错:

QNetworkReplyImplPrivate::error: Internal problem, this method must only be called once.

此时应该

m_reply->deleteLater();
m_reply = nullptr;

你可能感兴趣的:(QT)