概述
一,利用QtSoap 第三方库方式
参考:GitHub - commontk/QtSOAP: The Qt SOAP project provides basic web service support with version 1.1 of the SOAP protocol. It is fork of
二,利用 http post方式,自己拼接xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://portal.vidyo.com/user/v1_1">
<soapenv:Header/>
<soapenv:Body>
<v1:SearchRequest>
<v1:Filter>
<v1:query>liunianji</v1:query>
</v1:Filter>
</v1:SearchRequest>
</soapenv:Body>
</soapenv:Envelope>
在一些复杂的xml格式下,如上述格式 <v1:query> 上层有好多父节点, 利用第一种没有搞定 所以选择了第二种方式
(1),拼接 xml
QDomDocument doc;
QDomElement tagStart = doc.createElement("soapenv:Envelope");
tagStart.setAttribute("xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
tagStart.setAttribute("xmlns:v1","http://portal.vidyo.com/user/v1_1");
QDomElement tagHeader = doc.createElement("soapenv:Header");
QDomElement tagBody = doc.createElement("soapenv:Body");
doc.appendChild(tagStart);
tagStart.appendChild(tagHeader);
tagStart.appendChild(tagBody);
QDomElement tagSearchRequest = doc.createElement("v1:SearchRequest");
QDomElement tagFilter = doc.createElement("v1:Filter");
QDomElement tagQuery = doc.createElement("v1:query");
QDomText queryName = doc.createTextNode(displayName);
tagBody.appendChild(tagSearchRequest);
tagSearchRequest.appendChild(tagFilter);
tagFilter.appendChild(tagQuery);
tagQuery.appendChild(queryName);
(2),post 发送
QString webSite = QString("%1/services/v1_1/VidyoPortalUserService/").arg(m_portal);
QByteArray postBa = doc.toByteArray();
QByteArray retBa;
QNetworkReply::NetworkError ret=WebHelp::getInstance()->sendPostRequest(webSite,postBa,retBa);
if(ret==QNetworkReply::NoError)
{
qDebug()<< QString(retBa);
return parseSearchClassRe(retBa);
}
else
{
qDebug()<< QStringLiteral("网络错误,请检查网络!")<<ret;
}
三,封装好的 单例类
class
WebHelp:public QObject
{
Q_OBJECT
private:
class Dispose
{
public:
~Dispose();
};
public:
static WebHelp * getInstance();
~WebHelp();
public:
QNetworkReply::NetworkError sendGetRequest(QString urlStr, QByteArray & ba, int timeOutms=3000);
QNetworkReply::NetworkError sendPostRequest(QString website,const QByteArray & postBa, QByteArray& retBa, int timeOutms=3000);
protected slots:
void slot_error(QNetworkReply::NetworkError code);
void provideAuthenication(QNetworkReply*,QAuthenticator*);
private:
static WebHelp * serverHelp;
static WebHelp::Dispose disp;
QNetworkAccessManager *manager;
QNetworkReply::NetworkError m_errCode;
QString m_pwd;
QString m_user;
private:
WebHelp(QObject *parent=0);
};
class NetDataListenerThread:public QThread
{
Q_OBJECT
public:
NetDataListenerThread(QNetworkAccessManager *manager,QNetworkReply * reply,int timeoutms,QObject *parent = 0);
~NetDataListenerThread();
QByteArray getBa() const;
bool getIsWaitTimeOut() const;
protected:
virtual void run();
private:
bool waitForConnect();
protected slots:
void slot_waitTimeout();
signals:
void sig_waitTimeout();
private:
QNetworkAccessManager *mManager;
int m_TimeOutms;
bool m_waitTimeOut;
bool m_isWaitTimeOut;
QNetworkReply * m_reply;
};
NetDataListenerThread::NetDataListenerThread(QNetworkAccessManager *manager, QNetworkReply *reply, int timeoutms, QObject *parent)
:QThread(parent),mManager(manager),m_TimeOutms(timeoutms),m_reply(reply)
{
m_waitTimeOut = false;
m_isWaitTimeOut = false;
}
NetDataListenerThread::~NetDataListenerThread()
{
}
void NetDataListenerThread::run()
{
m_isWaitTimeOut = waitForConnect();
}
bool NetDataListenerThread::waitForConnect()
{
QEventLoop eventLoop;
QTimer * timer = nullptr;
m_waitTimeOut = false;
bool bWaitTimeOut = false;
if (m_TimeOutms > 0)
{
timer = new QTimer;
connect(timer, SIGNAL(timeout()), this, SLOT(slot_waitTimeout()));
timer->setSingleShot(true);
timer->start(m_TimeOutms);
connect(this, SIGNAL(sig_waitTimeout()), &eventLoop, SLOT(quit()));
}
connect(mManager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
if (m_reply != nullptr)
{
connect(m_reply, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
}
eventLoop.exec();
if (timer != nullptr)
{
timer->stop();
delete timer;
timer = nullptr;
}
bWaitTimeOut = m_waitTimeOut;
m_waitTimeOut = false;
return !bWaitTimeOut;
}
void NetDataListenerThread::slot_waitTimeout()
{
m_waitTimeOut = true;
emit sig_waitTimeout();
}
bool NetDataListenerThread::getIsWaitTimeOut() const
{
return m_isWaitTimeOut;
}
WebHelp * WebHelp::serverHelp = nullptr;
WebHelp::Dispose WebHelp::disp;
WebHelp::WebHelp(QObject *parent):QObject(parent)
{
m_user = SetupWidget::gConfig.user();
m_pwd = SetupWidget::gConfig.pwd();
manager = new QNetworkAccessManager();
m_errCode= QNetworkReply::NoError;
connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
SLOT(provideAuthenication(QNetworkReply*,QAuthenticator*)));
}
WebHelp::~WebHelp()
{
manager->deleteLater();
}
WebHelp *WebHelp::getInstance()
{
if(serverHelp==nullptr)
{
serverHelp = new WebHelp();
}
return serverHelp;
}
QNetworkReply::NetworkError WebHelp::sendGetRequest(QString urlStr, QByteArray & ba,int timeOutms)
{
QNetworkReply::NetworkError retError = QNetworkReply::NoError;
m_errCode= QNetworkReply::NoError;
QNetworkRequest request;
QUrl url(urlStr);
request.setUrl(url);
QNetworkReply *reply = manager->get(request);
connect(reply,static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),this,&WebHelp::slot_error);
QEventLoop eventLoop;
NetDataListenerThread * thread = new NetDataListenerThread(manager,reply,timeOutms);
connect(thread, &NetDataListenerThread::finished,&eventLoop,&QEventLoop::quit);
thread->start();
eventLoop.exec();
if(thread->getIsWaitTimeOut())
{
ba = reply->readAll();
}
else
{
m_errCode=QNetworkReply::TimeoutError;
}
thread->deleteLater();
delete reply;
delete thread;
thread = nullptr;
retError = m_errCode;
m_errCode= QNetworkReply::NoError;
return retError;
}
QNetworkReply::NetworkError WebHelp:: sendPostRequest(QString website, const QByteArray &postBa, QByteArray &retBa, int timeOutms)
{
QString urlStr =website;
QNetworkReply::NetworkError retError = QNetworkReply::NoError;
m_errCode= QNetworkReply::NoError;
QNetworkRequest request;
QSslConfiguration config;
config.setPeerVerifyMode(QSslSocket::VerifyNone);
config.setProtocol(QSsl::TlsV1SslV3);
request.setSslConfiguration(config);
QUrl url(urlStr);
request.setUrl(url);
request.setHeader( QNetworkRequest::ContentTypeHeader, QVariant(QString("text/xml;charset=utf-8")));
request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(qulonglong(postBa.size()) ));
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant( int(QNetworkRequest::AlwaysNetwork)));
QNetworkReply *reply = manager->post(request,postBa);
connect(reply,static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),this,&WebHelp::slot_error);
QEventLoop eventLoop;
NetDataListenerThread * thread = new NetDataListenerThread(manager,reply,timeOutms);
connect(thread, &NetDataListenerThread::finished,&eventLoop,&QEventLoop::quit);
thread->start();
eventLoop.exec();
if(thread->getIsWaitTimeOut())
{
retBa = reply->readAll();
}
else
{
m_errCode=QNetworkReply::TimeoutError;
}
thread->deleteLater();
delete reply;
delete thread;
thread = nullptr;
retError = m_errCode;
m_errCode= QNetworkReply::NoError;
return retError;
}
void WebHelp::slot_error(QNetworkReply::NetworkError code)
{
m_errCode = code;
}
void WebHelp::provideAuthenication(QNetworkReply *, QAuthenticator *ator)
{
ator->setUser(m_user);
ator->setPassword(m_pwd);
}
WebHelp::Dispose::~Dispose()
{
if(WebHelp::serverHelp!=nullptr)
{
delete WebHelp::serverHelp;
WebHelp::serverHelp=nullptr;
}
}
四,备注
(1),如果网站需要用户名 和密码 需要再此处设置
void WebHelp::provideAuthenication(QNetworkReply *, QAuthenticator *ator)
{
ator->setUser(m_user);
ator->setPassword(m_pwd);
}
(2),两个 header 可能略有不同
request.setHeader( QNetworkRequest::ContentTypeHeader, QVariant(QString("text/xml;charset=utf-8")));
request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(qulonglong(postBa.size()) ));
最后
以上就是无心画笔为你收集整理的Qt 访问 webservice 服务的全部内容,希望文章能够帮你解决Qt 访问 webservice 服务所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复