QT QHttp 访问 WEBSERVICE 范例

来源:互联网 发布:动量交易系统 源码 编辑:程序博客网 时间:2024/05/16 06:16
根据 SOAP 1.1 标准,POST XML 来访问 WEBSERVICE ,会非常方便,QT 本身有 XML 的解析工具QDomDocument/ QXmlReader。将 POST 的结果进行处理也是非常方便的。

实际应用的时候,只要更改 URL、SOAPAction 、参数、NameSpace 等必要的元素,即可。

        QUrl url(urlLineEdit->text());
    QHttpRequestHeader header("POST", url.path() );
    header.setValue(
"Host", url.host() );
    header.setContentType(
"text/xml; charset=utf-8");
    header.setValue(
"SOAPAction"""http://tempuri.org/HelloWorld"");

    QString content(
"<?xml version="1.0" encoding="utf-8"?>"
                    
"<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">"
                    "  <soap:Body>"
                    
"    <HelloWorld xmlns="http://tempuri.org/">sdafwer</HelloWorld>"
                    
"  </soap:Body>"
                    
"</soap:Envelope>"
                    );
    
//header.setValue("Content-Length", tr("%1").arg(11) );
    http->setHost( url.host() );

    httpRequestAborted 
= false;
     httpGetId 
= http->request( header, content.toUtf8 (), buffer);

 本文仅示范如何得到 WEBSERVICE 的结果,没有将结果进行处理。