在 QNetworkRequest 中设置标头时遇到问题

发布于 2024-10-18 04:41:30 字数 1277 浏览 1 评论 0原文

我正在使用以下函数发出发布请求:

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 to
QNetworkRequest::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2024-10-25 04:41:30

问题出在第二个参数 (USERAGENT) 中。 QByteArray 可以从 char 数组构造,但不能从 QString 构造(请参阅 QByteArray 的文档)。您需要使用 QString::toAscii() QString::toLatin1() 或类似的东西,或者使 USERAGENT 成为 char*

 const char* USERAGENT = "Mozilla/Firefox 3.6.12";

The problem is in the second parameter (USERAGENT). A QByteArray can be constructed from a char array, but not from a QString (see QByteArray's documentation). You need either to use QString::toAscii(), QString::toLatin1() or something similar, or to make USERAGENT a char*:

 const char* USERAGENT = "Mozilla/Firefox 3.6.12";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文