-
Notifications
You must be signed in to change notification settings - Fork 0
/
mynetwork.cpp
47 lines (40 loc) · 1.42 KB
/
mynetwork.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "mynetwork.h"
#include <iostream>
#include <string>
#include <QUrl>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QApplication>
MyNetwork::MyNetwork()
{
postData.append("auth=bsap");
theResponse = "";
}
void MyNetwork::setPost(const QString &theKey,const QString &theValue)
{
postData.append("&").append(theKey).append("=").append(theValue);
}
void MyNetwork::sendPost()
{
QUrl url("http://webservices.pongsit.com/cs340/index.php");
QNetworkRequest request = QNetworkRequest(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36");
QNetworkAccessManager * TheNetworkManager = new QNetworkAccessManager(this);
connect(TheNetworkManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(setResponse(QNetworkReply *)));
//std::cout << "3: " << QString(postData).toStdString() << std::endl;
TheNetworkManager->post(request,postData);
}
void MyNetwork::setResponse(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError){
QByteArray response = reply->readAll();
theResponse = response;
}else{
theResponse = "Cannot connect to the server.";
qDebug() << reply->error();
}
emit donePost(this);
}