我正在尝试使用谷歌的 url 缩短器 api ( https://developers.google.com/url-shortener/v1/getting_started#shorten ) 创建一个 url 缩短器应用程序。问题是我收到了一个错误的请求。这是错误: 错误:下载https://www.googleapis.com/urlshortener/v1/url时出错- 服务器回复:错误请求
我做错了什么?这是代码:
void MainWindow::ppp(QString longurl)
{
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
QUrl url = QUrl("https://www.googleapis.com/urlshortener/v1/url");
QNetworkRequest request(url);
request.setHeader(request.ContentTypeHeader,"application/json");
QByteArray postData;
postData.append("longUrl");
postData.append(longurl);
manager->post(request,postData);
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
if(reply->error() != QNetworkReply::NoError)
{
ui->textBrowser->setText("Error: " + reply->errorString());
}
else
{
QByteArray responseData = reply->readAll();
QString qstr(responseData);
ui->textBrowser->setText(qstr);
}
}