QtSoap开发web services客户端程序

首先需要下载QtSoap开源包,下载地址为:

http://www.filestube.com/q/qtsoap+download

我使用的是:qtsoap-2.6-opensource(不需要安装,直接解压到某个目录即可)。

如果你从未使用过QtSoap,那么先学习其中的Demo,在目录"examples"中,有easter,google和population 三个例子。

Note to Qt Visual Studio Integration users: In the instructions below, instead of building from command line with nmake, you can use the menu command ‘Qt->Open Solution from .pro file‘ on the .pro files in the example and plugin directories, and then build from within Visual Studio.

如果是使用的的是VS+Qt开发方式,在使用菜单中的"Qt→Open Qt Project File (.pro)... 来打开以上工程文件。

本人使用的是VS2010,在采用以上方法时,仍然无法正常载入,于是我先用VS2005打开,在VS2005里对以上项目进行保存,再在VS2010里重新打开,当然在VS2010里重新打开时会提示需要进行格式转换,成功后你就可以编译运行Demo了。

本人先重写了:population,主要是为了熟悉下QtSoap以及开发配置。

1 在头文件(xxx.h)定义变量(XXX为类名,请您重新定义)。

#include

class QTextEdit;

private slots:
    void getResponse();

private:
    QTextEdit   *m_pWeatherEdt;

private:
    QtSoapHttpTransport http;

2 在类的构造函数中:

m_pWeatherEdt = new QTextEdit(this);

connect(&http, SIGNAL(responseReady()), SLOT(getResponse()));

3 请求发送:

void XXX::submitRequest()
{

http.setHost("www.abundanttech.com");
    QtSoapMessage request;

http.setAction  ("http://www.abundanttech.com/WebServices/Population/getPopulation");
    request.setMethod("getPopulation", "http://www.abundanttech.com/WebServices/Population");
    request.addMethodArgument("strCountry", "", "china");
    http.submitRequest(request, "/WebServices/Population/population.asmx");

}

以上参数请查看:http://www.abundanttech.com/WebServices/Population/population.asmx

如下:

SOAP

The following is a sample SOAP request and response. The placeholders shown need to be replaced with actual values.

POST /WebServices/Population/population.asmx HTTP/1.1
Host: www.abundanttech.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.abundanttech.com/WebServices/Population/getPopulation"

      string

当然,你也可以使用“中国气象局”的web services来做测试示例

void XXX::submitRequest()
{

http.setHost("www.ayandy.com");
    QtSoapMessage request;

http.setAction("http://tempuri.org/getSupportProvince");
    request.setMethod("getSupportProvince", "http://tempuri.org/");
    http.submitRequest(request, "/Service.asmx");

}

以上参数请查看:http://www.ayandy.com/Service.asmx如下:

SOAP 1.1

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /Service.asmx HTTP/1.1
Host: www.ayandy.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/getSupportProvince"

4 回复解析:

void XXX::getResponse()
{

// Get a reference to the response message.

const QtSoapMessage &message = http.getResponse();

// Check if the response is a SOAP Fault message
    if (message.isFault()) 
    {
        m_pWeatherEdt->append(message.faultString().value().toString().toLatin1().constData());
    }
    else 
    {
        // Get the return value, and print the result.
        const QtSoapType &response = message.returnValue();
        m_pWeatherEdt->append(response["Country"].value().toString().toLatin1().constData());
        m_pWeatherEdt->append(response["Pop"].value().toString().toLatin1().constData());
        m_pWeatherEdt->append(response["Date"].value().toString().toLatin1().constData());
    }
}

以上方法只能解析固定类型的消息,下面介绍一种通用方法:

void XXX::getResponse()
{

// Get a reference to the response message.
    const QtSoapMessage &message = http.getResponse();

// Check if the response is a SOAP Fault message
    if (message.isFault()) 
    {
        m_pWeatherEdt->append(message.faultString().value().toString().toLatin1().constData());
    }
    else 
    {
        const QtSoapType &root = message.returnValue();
        QtSoapStruct myStruct((QtSoapStruct&)root);

//将返回的结构体转换成QtSoapStruct
        for (int i = 0; i < myStruct.count(); i++)
        {
            m_pWeatherEdt->append(myStruct[i].typeName() + " : " + myStruct[i].toString());
        }

}
}

第二种解析方法参考文档:http://hi.baidu.com/sungaoyong/blog/item/76d9beef0983503762d09fce.html/cmtid/aa176588dfdf021cc8fc7a83

http://blog.chinaunix.net/uid-20718335-id-364410.html

时间: 2024-08-02 14:26:42

QtSoap开发web services客户端程序的相关文章

Qt 直接构建XML开发web services客户端程序

client.h   client.cpp     :   XML发送与接收 service.h service.cpp  :   消息的发送与接收 message.h message.cpp    :   消息的构建 messageparser.h messageparser.cpp  :   消息的解析 calc.h calc.cpp    :   计算 add.h add.cpp    :   加法运算 源代码下载:http://download.csdn.net/detail/chenj

第二章 基于JAX-WS开发Web services

基于JAX-WS开发Web services 这篇文章纯属搬砖. 转载于:http://www.ithov.com/linux/125942_4.shtml. Web Services 是一种面向服务的技术,通过标准的 Web 协议提供服务,目的是保证不同平台的应用服务可以互操作.依据 web services 规范实施的应用之间,无论它们所使用的语言. 平台或内部协议是什么,都可以相互交换数据,这就是 web services 的好处.本文选择 IBM WebSphere Applicatio

xFire 开发web services

1.首先xfire 不能加紧MyEclipse10中 只能通过添加jar包的方式添加: 2.给MyEclipse6.5 添加xfire 点击 help->software updates ->find and install ->选择 search for new features to install 选择第二个  然后 下一步: 添加site  url:http://dist.codehaus.org/xfire/update/ 如图: 3.新建 web service projec

gSOAP 开发Web Service 客户端应用程序--摘自gSOAP文档

calc.wsdl 此案例使用c++开发 $>wsdl2h -o calc.h http://www.genivia.com/calc.wsdl $>soapcpp2 -i -C calc.h 这个时候你的文件应该有: 注意:去除.o结尾的文件和e.cpp剩下的文件就是用soapcpp2所产生的文件 $>touch e.cpp;vi e.cpp e.cpp 代码: $>g++ –c soapC.cpp --会产生soapC.o文件 $>g++ –c e.cpp --会产生e.

跟我一起学WCF(3)——利用Web Services开发分布式应用

一.引言 在前面文章中分别介绍了MSMQ和.NET Remoting技术,今天继续分享.NET 平台下另一种分布式技术——Web Services 二.Web Services 详细介绍 2.1 Web Services 概述 Web Services是支持客户端与服务器通过网络互操作的一种软件系统,是一组可以通过网络调用的应用程序API.在Web Services中主要到SOAP/UDDI/WSDL这三个核心概念,下面分别介绍下这三个概念的定义. SOAP:SOAP(Simple Object

利用Web Services开发分布式应用

一.引言 在前面文章中分别介绍了MSMQ和.NET Remoting技术,今天继续分享.NET 平台下另一种分布式技术——Web Services 二.Web Services 详细介绍 2.1 Web Services 概述 Web Services是支持客户端与服务器通过网络互操作的一种软件系统,是一组可以通过网络调用的应用程序API.在Web Services中主要到SOAP/UDDI/WSDL这三个核心概念,下面分别介绍下这三个概念的定义. SOAP:SOAP(Simple Object

利用WSCF进行契约先行的Web Services开发

http://www.cnblogs.com/goody9807/archive/2007/06/05/772107.html 什么是契约先行(Contract-First)? 如果说一个新的软件开发模型会影响甚至改变软件开发过程,那么这样的改变对于开发人员而言,无疑是最具有深远意义的.多年以来,大多分布式应用程序的开发人员都习惯关注对象和从头开始编写一大堆的代码.然而,在面向服务(Service-Oriented)的时间,一切都将改变.从面向过程到面向对象,再到基于组件的开发(Componen

使用CXF开发Web Service服务

1.使用CXF开发Web Service服务端 1.1 开发一个Web Service业务接口,该接口要用@WebService修饰 (1)创建一个Java项目MyServer (2)在MyServer项目中创建一个接口HelloWorld package com.xju.ws; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHello(String name); } 1.2 开发

Web services 安全 - HTTP Basic Authentication

根据 RFC2617 的规定,HTTP 有两种标准的认证方式,即,BASIC 和 DIGEST.HTTP Basic Authentication 是指客户端必须使用用户名和密码在一个指定的域 (Realm) 中获取认证. 正如"HTTP Basic Authentication"这个名字,它是 Authentication( 认证 ) 中最简单的方法.长期以来,这种认证方法被广泛的使用.当你通过 HTTP 协议去访问一个使用 Basic Authentication 保护的资源时,服