简体   繁体   中英

Having trouble setting headers in a QNetworkRequest

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM