예제 #1
0
/**
 * Send a POST request with data to the given URL.
 *
 * \param[out] buffer The buffer where the response will be written.
 * \param[in] bufferSize The size of the output buffer.
 * \param[in] host The remote host where the resource is located.
 * \param[in] path The path of the desired resource on the host.
 *
 * \return The number of bytes actually received, -1 in case of failure.
 *
 * \note host can be either the domain name or the IP address of the host.
 */
int HttpClient::post(char* buffer, size_t bufferSize, const char* host,
    const char* path, const char* content) {
    // generate the HTTP request
    if (!createPostRequest(buffer, bufferSize, host, path, content,
        (F_POST | F_KEEP_ALIVE))) {
        return -1;
    }
    // try to send the request and wait for a response from the host
    wifly_->clear();
    if (!wifly_->print(buffer))
        return -1;
    wifly_->flush();

    // clear the buffer since it still holds the HTTP request
    memset(buffer, 0x00, bufferSize);
    if (!wifly_->awaitResponse())
        return -1;

    // skip the headers
    if (!wifly_->find_P(HTTP_END_OF_HEADER))
        return -1;

    int nBytes = wifly_->readBytes(buffer, bufferSize);
    return nBytes;
}
예제 #2
0
QNetworkReply * TwitterQueryUtil::post(QNetworkAccessManager &network,
                                       const QString &path,
                                       const std::map<QByteArray, QByteArray> &parameters,
                                       const std::map<QByteArray, QByteArray> &postData,
                                       const Account &account)
{
    QNetworkRequest request {createPostRequest(path, parameters, postData, account)};
    QUrlQuery postDataQuery {};
    for (const std::pair<QByteArray, QByteArray> &parameter : postData) {
        postDataQuery.addQueryItem(QLatin1String(parameter.first), QLatin1String(parameter.second));
    }
    return network.post(request, postDataQuery.toString(QUrl::FullyEncoded).toLatin1());
}