这是我正在使用的功能/方法。
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: error: no matching function for call to ‘QNetworkRequest::setRawHeader(const char [11], QString&)’
它需要2个QByteArrays作为参数,官方示例甚至将它传递给两个字符串:
request.setRawHeader("Last-Modified", "Sun, 06 Nov 1994 08:49:37 GMT");
不是它应该重要,但我已经尝试过但仍然得到同样的错误。我的Qt坏了吗?
答案 0 :(得分:2)
问题出在第二个参数(USERAGENT)中。 QByteArray可以从char数组构造,但不能从QString构造(参见QByteArray's documentation)。您需要使用QString::toAscii(),QString :: toLatin1()或类似的东西,或者使USERAGENT成为char *:
const char* USERAGENT = "Mozilla/Firefox 3.6.12";