开发者

Having trouble setting headers in a QNetworkRequest

开发者 https://www.devze.com 2023-02-12 18:45 出处:网络
I am using the following function to make a post request: bool NewAccountDialog::verifyAccount() { QString loginURL = \"https://accounts.craigslist.org/\";

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 c开发者_如何学Pythonall:

/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?


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";
0

精彩评论

暂无评论...
验证码 换一张
取 消