0

我正在尝试使用 Qt 访问以下 URL:“ https://www.cyclestreets.net/api/journey.json?itinerarypoints=13.417365,52.513381%7C13.438596,52.519854&key=cdccf13997d59e70&plan=balanced&reporterrors=1&speed=20 ”。如果我在网络浏览器中点击它,它确实会给出一些响应(请自行尝试)。QNetworkReply::NetworkError(UnknownNetworkError)但是,当我尝试使用以下代码进行相同操作时收到错误消息:

QUrl url("https://www.cyclestreets.net/api/journey.json");
QMap<QString, QString> queryStrings;
queryStrings["key"] = "cdccf13997d59e70";
queryStrings["reporterrors"] = QLatin1Char('1');
queryStrings["plan"] = settings[QStringLiteral("plan")].toString();
if (queryStrings["plan"].isEmpty()) {
    queryStrings["plan"] = QStringLiteral("balanced");
}
queryStrings["speed"] = settings[QStringLiteral("speed")].toString();
if (queryStrings["speed"].isEmpty()) {
    queryStrings["speed"] = QStringLiteral("20");
}
GeoDataCoordinates::Unit const degree = GeoDataCoordinates::Degree;
QString itinerarypoints;
itinerarypoints.append(QString::number(route->source().longitude(degree), 'f', 6) + QLatin1Char(',') + QString::number(route->source().latitude(degree), 'f', 6));
for ( int i=1; i<route->size(); ++i ) {
    itinerarypoints.append(QLatin1Char('|') +  QString::number(route->at(i).longitude(degree), 'f', 6) + QLatin1Char(',') + QString::number(route->at(i).latitude(degree), 'f', 6));
}
queryStrings["itinerarypoints"] = itinerarypoints;

QUrlQuery urlQuery;
for( const QString& key: queryStrings.keys()){
    urlQuery.addQueryItem(key, queryStrings.value(key));
}
url.setQuery( urlQuery);

m_request.setUrl( url );
m_request.setRawHeader( "User-Agent", HttpDownloadManager::userAgent( "Browser", "CycleStreetsRunner" ) );
QNetworkReply *reply = m_networkAccessManager.get( m_request );
connect( reply, SIGNAL(error(QNetworkReply::NetworkError)),
         this, SLOT(handleError(QNetworkReply::NetworkError)), Qt::DirectConnection );

void CycleStreetsRunner::handleError( QNetworkReply::NetworkError error )
{
    qDebug() << "Error: " << error;
}

知道这怎么可能发生?完全相同的 URL 怎么可能在 Web 浏览器中给出有效响应,但在 Qt 中给出网络错误?任何类型的帮助将不胜感激。

4

0 回答 0