Qt:https get(),实现调网站短信接口发送短信,json解析

需要实现qt走https,调网站的短信接口,踩了坑,记一下。

代码:

void SendMessage()
{
	QNetworkAccessManager *manager;
	manager = new QNetworkAccessManager(this);

	connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinishedRes((QNetworkReply *))); 

	QString phone = "123456";
	QString url = QString("https://www.123/test.php?phone=%1").arg(phone);
	QNetworkRequst networkRequest;
	networkRequest.setUrl(url);

    //如果走的HTTP协议,这一段就可以不要,否则失败
	QSslConfiguration sslConfig = QSslConfiguration::defaultConfiguration();
	sslConfig.setPeerVerifyMode(QSslSocked::VerifyNone);
	sslConfig.setProtocol(QSsl::TlsV1_2);
	networkRequst.setSslConfiguration(sslConfig);

    //这里是get()
	manager->get(netWorkRequest);
}

//槽函数,相应网络回调
void replyFinishedRes(QNetworkReply *reply)
{
    //网络错误,https协议时没加ssl会报错
	if(reply->error() != QNetworkReply::NoError)
	{
		return;
	}

	int CodeRes;
	QString Message;

	QTextCode *codec = QTextCodec::codeForName("utf8");

	QByteArray data = reply->readAll();

	QJsonParseError jsonError;
	QJsonDocument jsonDocument = QJsonDocument::fromJson(data, &jsonError);
	if(jsonError.error == QJsonParseReeor::NoError)
	{
		if(jsonDocument.isObject)
		{
			QJsonObject object = jsonDocument.object();
			if(object.comtains("retcode"))
			{
				QJsonValue value = object.take("retcode");
				if(value.isDouble())
				{	
					CodeRes = value.toInt();
				}
			}
		}
	}
	
	reply->deleteLater();
}

踩得坑就是一开始没加ssl,测试服的是http协议,所以一下就成功了,正式服是https,总是报错

你可能感兴趣的:(QT,c++)