QNetworkAccessManager 클래스를 사용하려면 프로젝트파일에 먼저 아래를 명시해야한다.
QT += network
아래 예제는 QNetworkAccessManager를 통해 HTTP POST 하는 방법을 보여준다.
void post(){
// Build your JSON string as usual
QByteArray jsonString = "{\"method\":\"AuthenticatePlain\",\"loginName\":\"username@domain.com\",\"password\":\"mypass\"}";
// For your "Content-Length" header
QByteArray postDataSize = QByteArray::number(jsonString.size());
// Time for building your request
QUrl serviceURL("https://www.superService.com/api/1.7/ssapi.asmx");
QNetworkRequest request(serviceURL);
// Add the headers specifying their names and their values with the following method : void QNetworkRequest::setRawHeader(const QByteArray & headerName, const QByteArray & headerValue);
request.setRawHeader("User-Agent", "My app name v0.1");
request.setRawHeader("X-Custom-User-Agent", "My app name v0.1");
request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("Content-Length", postDataSize);
// Use QNetworkReply * QNetworkAccessManager::post(const QNetworkRequest & request, const QByteArray & data); to send your request. Qt will rearrange everything correctly.
QNetworkReply * reply = m_qnam->post(request, jsonString);
}