我是新手,我正在尝试使用Qt4.8将.xml文件发送到服务器。我的限制是,我被告知只能使用QNetworkAccessManager->get()
。尽管QNetworkAccessManager->post
可用,但我不能按照发给我的说明使用它。因此,编写以下代码:-
bool PIS::sendPISData(QString xmlFile,QString IP)
{
QFile xmlfyle(xmlFile);
if(!xmlfyle.open(QIODevice::ReadOnly))
{
#ifdef DEBUG
qDebug("Can not open file device.");
#endif
}
QString content = (QString)xmlfyle.readAll();
xmlfyle.close();
QNetworkAccessManager mgr;
QEventLoop eventLoop;
QUrl url(QString("http://"+IP+"/Smart_Coach_Service/data_interchange/senddata"));
QNetworkRequest reqpn(url);
reqpn.setRawHeader("Content-Type", "application/xml");
reqpn.setRawHeader("Content-Length",QByteArray::number(content.toUtf8().size()));
reqpn.setRawHeader("Content-Data",content.toUtf8());
QNetworkReply *replyn = mgr.get(reqpn);
connect(replyn, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec(); // blocks stack until "finished()" has been called
return true;
}
但是我在服务器端什么也没得到。我想知道我到底在想什么
答案 0 :(得分:0)
我建议您阅读有关HTTP的更多信息。尽管有机会使用BASE64编码(HTTP不允许在标头中使用原始UTF8)在自定义标头(如您的"Content-Data"
)中发送数据,但这就是所谓的 BAD PRACTICE 。不要这样做!
答案 1 :(得分:0)
我知道了,您必须添加数据作为查询。代码将如下所示:-
bool PIS::sendPISData(QString xmlFile,QString IP)
{
QFile xmlfyle(xmlFile);
if(!xmlfyle.open(QIODevice::ReadOnly))
{
#ifdef DEBUG
qDebug("Can not open file device.");
#endif
}
QString content = (QString)xmlfyle.readAll();
#ifdef DEBUG
qDebug()<<"content ::: "<<content;
#endif
xmlfyle.close();
QNetworkAccessManager mgr;
QEventLoop eventLoop;
QUrl url(QString("http://"+IP+"/Smart_Coach_Service/data_interchange/senddata"));
url.addQueryItem("xml_data", content);
QNetworkReply *replyn = mgr.get(reqpn);
connect(replyn, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();
return true;
}