今天学习如何用QtSoap访问Web Service服务。这里调用的是查询QQ在线状态的服务qqOnlineWebService。调用的几个步骤:
1.创建QtSoapMessage对象 message;
2.message调用setMethod设置调用的服务
3.message调用addMethodArgument设置参数
4.创建QtSoapHttpTransport *m_pHttp
5.m_pHttp调用setHost,setAction
6.最后m_pHttp调用submitRequest提交请求
class WebServiceClient : public QDialog
{
Q_OBJECT
public:
WebServiceClient(QWidget *parent = 0,Qt::WFlags flags = 0);
~WebServiceClient();
private slots:
voidSubmitSlot();
voidResponseSlot(const QtSoapMessage &response);
private:
voidresizeEvent(QResizeEvent *event);
QLineEdit *m_pNumber;
QPushButton *m_pSubmit;
QLineEdit *m_pResult;
QLabel *m_pQQNumber;
QLabel *m_pState;
QtSoapHttpTransport *m_pHttp;
};
WebServiceClient::WebServiceClient(QWidget*parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
m_pQQNumber = newQLabel(tr("QQ号码"),this);
m_pState = newQLabel(tr("QQ状态"),this);
m_pNumber = newQLineEdit(this);
m_pResult = newQLineEdit(this);
m_pResult->setReadOnly(true);
m_pSubmit = newQPushButton(tr("提交"),this);
m_pHttp = newQtSoapHttpTransport;
connect(m_pSubmit,SIGNAL(clicked()),this,SLOT(SubmitSlot()));
connect(m_pHttp,SIGNAL(responseReady( const QtSoapMessage & )),this,SLOT(ResponseSlot(constQtSoapMessage &)));
this->setFixedSize(QSize(300,300));
}
WebServiceClient::~WebServiceClient()
{
}
voidWebServiceClient::ResponseSlot( constQtSoapMessage & response )
{
QString xml = response.toXmlString();
QDomDocument doc;
doc.setContent(xml);
QDomNodeList nodeList =doc.elementsByTagName("qqCheckOnlineResult");
if(nodeList.size())
{
QDomNode resultNode =nodeList.at(0);
QString result =resultNode.toElement().text();
QString state;
if(QString::compare(result,"Y") ==0)
{
state = tr("在线");
}
elseif (QString::compare(result,"N") == 0)
{
state = tr("离线");
}
elseif (QString::compare(result,"E") == 0)
{
state = tr("QQ号码错误");
}
elseif (QString::compare(result,"A") == 0)
{
state = tr("商业用户验证失败");
}
elseif (QString::compare(result,"V") == 0)
{
state = tr("免费用户超过数量");
}
m_pResult->setText(state);
}
}
voidWebServiceClient::SubmitSlot()
{
QtSoapMessage message;
message.setMethod("qqCheckOnline","http://WebXml.com.cn/");
m_pHttp->setAction("http://WebXml.com.cn/qqCheckOnline");
m_pHttp->setHost("webservice.webxml.com.cn");
QString number = m_pNumber->text();
message.addMethodArgument("qqCode","",number);
QString xml = message.toXmlString();
m_pHttp->submitRequest(message,"/webservices/qqOnlineWebService.asmx");
}
voidWebServiceClient::resizeEvent( QResizeEvent *event)
{
m_pQQNumber->setGeometry(5,5,50,20);
m_pNumber->setGeometry(60,5,100,20);
m_pSubmit->setGeometry(160,5,50,20);
m_pState->setGeometry(5,30,50,20);
m_pResult->setGeometry(60,30,100,20);
}
int main(int argc, char*argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
WebServiceClient w;
w.show();
returna.exec();
}
http://blog.csdn.net/hai200501019/article/details/19755987