在 QNetworkRequest 中设置标头时遇到问题
我正在使用以下函数发出发布请求:
bool NewAccountDialog::verifyAccount()
{
QString loginURL = "https://accounts.craigslist.org/";
QString USERAGENT = "Mozilla/Firefox 3.6.12";
// This is all bullshit
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setUrl(loginURL);
request.setRawHeader("User-Agent", USERAGENT);
QByteArray data;
QUrl params;
params.addQueryItem("inputEmailHandle", accountName->text());
params.addQueryItem("inputPassword", accountPass->text());
data.append(params.toString());
//No idea what this does
data.remove(0,1);
QNetworkReply *reply = manager->post(request,data);
// Parse reply
return 1;
}
显然它不完整,但它不会编译,在我运行 request.setRawHeader()
时出现错误,抱怨没有与我的调用匹配的函数:
/home/brett/projects/CLPoster/CLPoster-build-desktop/../CLPoster/newaccountdialog.cpp:120: 错误:没有匹配的函数可供调用
QNetworkRequest::setRawHeader(const char [11], QString&)
它需要 2 个 QByteArrays
作为参数,官方示例甚至传递了两个字符串:
request.setRawHeader("Last-Modified", "Sun, 06 Nov 1994 08:49:37 GMT");
这并不重要,但我已经尝试过,但仍然遇到相同的错误。我的Qt坏了吗?
I am using the following function to make a post request:
bool NewAccountDialog::verifyAccount()
{
QString loginURL = "https://accounts.craigslist.org/";
QString USERAGENT = "Mozilla/Firefox 3.6.12";
// This is all bullshit
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setUrl(loginURL);
request.setRawHeader("User-Agent", USERAGENT);
QByteArray data;
QUrl params;
params.addQueryItem("inputEmailHandle", accountName->text());
params.addQueryItem("inputPassword", accountPass->text());
data.append(params.toString());
//No idea what this does
data.remove(0,1);
QNetworkReply *reply = manager->post(request,data);
// Parse reply
return 1;
}
Obviously it's incomplete, but it doesn't compile giving me an error where I run request.setRawHeader()
complaining there is no function that matches my call:
/home/brett/projects/CLPoster/CLPoster-build-desktop/../CLPoster/newaccountdialog.cpp:120:
error: no matching function for call toQNetworkRequest::setRawHeader(const char [11], QString&)
It takes 2 QByteArrays
as parameters, and the official example even passes it two strings:
request.setRawHeader("Last-Modified", "Sun, 06 Nov 1994 08:49:37 GMT");
Not that it should matter, but I've tried that and still get the same error. Is my Qt broken?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在第二个参数 (
USERAGENT
) 中。QByteArray
可以从 char 数组构造,但不能从QString
构造(请参阅QByteArray
的文档)。您需要使用QString::toAscii()
、QString::toLatin1()
或类似的东西,或者使USERAGENT
成为char*
:The problem is in the second parameter (
USERAGENT
). AQByteArray
can be constructed from a char array, but not from aQString
(seeQByteArray
's documentation). You need either to useQString::toAscii()
,QString::toLatin1()
or something similar, or to makeUSERAGENT
achar*
: