Qt http post way“Protocol \”\“未知”

时间:2018-01-05 04:48:07

标签: qt http post

void MainWindow::start()
{
  QNetworkAccessManager* manager = new QNetworkAccessManager;
  QJsonObject obj;
  obj.insert("pro", 2005);
  obj.insert("id", 10010033);

  QNetworkRequest req;
  req.setUrl(QUrl("192.168.90.114:38080/udp/data"));
  req.setRawHeader("Accept-Encoding", "gzip, deflate");
  req.setRawHeader("Content-Type", "application/json");
  req.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
  qDebug() << QJsonDocument(obj).toJson();
  manager->post(req, QJsonDocument(obj).toBinaryData());
   QObject::connect(manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply)
  {
     if(reply->error() != QNetworkReply::NoError)
     {
       qDebug() << "Error:" << reply->errorString();
       return;
     }
     QByteArray buf = reply->readAll();
     qDebug() << "OK:"<< buf;
  });
}

以下是错误信息

"{\n    \"id\": 10010033,\n    \"pro\": 2005\n}\n"
Error: "Protocol \"\" is unknown"

当我使用邮差测试这个界面时,结果如下图所示

enter image description here

1 个答案:

答案 0 :(得分:0)

由于我遇到了完全相同的问题,因此问题在于QUrl部分。 我用QUrl::fromUserInput(address)来解决问题(有人还建议::fromPercentEncoding)。