Qt中Http请求代码

HTTP/定义的请求方法有8种:GET(完整请求一个资源)、POST(提交表单)、PUT(上传文件)、DELETE(删除)、PATCH、HEAD(仅请求响应首部)、OPTIONS(返回请求的资源所支持的方法)、TRACE(追求一个资源请求中间所经过的代理)。最常的两种GET和POST,如果是RESTful接口的话一般会用到GET、POST、DELETE、PUT。

class HttpRequestpublic QObject
{
	Q_OBJECT
public:
	HttpRequest();
	~HttpRequest();
private:
	QString m_sValue;
public:
	QNetworkRequest requerst;
	QNetworkReply* reply;
public:
	void PostData();
	void Post(QString http,QString str);
}
void HttpRequest::PostData(QString http,QJsonObject obj)
{
	NetworkAccessManager* pNaManage = new NetworkAccessManager;
	request.setUrl(QUrl(http));
	request.setHeader(QNetworkRequest::ContentTypeHeader,QVarial("application/json"));
	QString testData = QJsonDocument(obj).toJson(QJsonDocument::Compact);
	reply = pNaManage->post(request,testData.toUtf8()); 
	connect(pNaManage,&QNetworkAccessManager::finished,[=](QNetworkReply* reply)
	{
		QString str = QString(reply->readAll());
	})
}
void HttpRequest::Post(QString http,QString str)
{
	NetworkAccessManager* NodePost = new NetworkAccessManager;
	request.setUrl(QUrl(http));
	request.setHeader(QNetworkRequest::ContentTypeHeader,QVarial("application/json"));
	QString sNode = str;
	reply = NodePost->post(request,sNode.toUtf8()); 
	connect(NodePost,&QNetworkAccessManager::finished,[=](QNetworkReply* reply)
	{
		QString str = QString(reply->readAll());
		emit SendData(str);
	})
}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页