soapclient调用webservice(soap服务调用)

http://www.itjxue.com  2023-01-26 09:32  来源:未知  点击次数: 

gsoap怎么调用webservice 接口函数

1、下载soap

2、解压到c:\gsoap-2.7

3、将c:\gsoap-2.7下的soapcpp2.exe,wsdl2h.exe,stdsoap2.h,stdsoap2.cpp拷贝到C:\wstest\client\gsoap2.7目录下

4、在C:\wstest\client\gsoap2.7目录下执行soapcpp2 -C -x test.h -I "C:/gsoap-2.7/gsoap/import"

5、在C:\wstest\client\gsoap2.7目录下执行wsdl2h -I "C:/gsoap-2.7/gsoap/WS" -s

-o test.h

6、将C:\wstest\client\gsoap2.7目录下文件拷贝到C:\wstest\client\test\目录下

7、新建立qt5项目,test.pro文件:

QT += core gui

QT +=network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test

TEMPLATE = app

SOURCES += main.cpp\

mainwindow.cpp \

stdsoap2.cpp \

soapC.cpp \

soapClient.cpp

HEADERS += mainwindow.h \

stdsoap2.h \

soapH.h \

stdsoap2.h \

soapStub.h

FORMS += mainwindow.ui

LIBS += -L . -l ws2_32

mainwindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include QHBoxLayout

#include "CWSCrmSoap.nsmap"

#include "soapCWSCrmSoapProxy.h"

#include "soapH.h"

wsdl2h常用选项

-o 文件名,指定输出头文件

-n 名空间前缀 代替默认的ns

-c 产生纯C代码,否则是C++代码

-s 不要使用STL代码

-t 文件名,指定type map文件,默认为typemap.dat

-e 禁止为enum成员加上名空间前缀

soapcpp2常用选项

-C 仅生成客户端代码

-S 仅生成服务器端代码

-L 不要产生soapClientLib.c和soapServerLib.c文件

-c 产生纯C代码,否则是C++代码(与头文件有关)

-I 指定import路径

-x 不要产生XML示例文件

-i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。

webservice调用方式http和soap的区别

项目的需求,需要和一个.net系统进行数据交换,合作方提供了一个WebService接口。这个与一般的PHP POST或GET传值再查库拿数据的思路有点不一样,需要用到SOAP模块,处理方法也很简单,就是有一些需要注意的事情。

首先确认你的PHP.ini开启了.SOAP,就是 extension=php_soap.dll 这前面的分号去咯。

代码很简单:

?php

$client = new SoapClient('');

$client-soap_defencoding = 'utf-8';

$client-decode_utf8 = false;

$client-xml_encoding = 'utf-8';

$param = array('param1'='01', 'param2'='02');

//$param["param1"]="01";

//$param["param2"]="02";

//$result = $client-__soapCall("GetArticle", array( $param ));

$result = $client-__Call("GetArticle", array( $param ));

if (is_soap_fault($result))

{

trigger_error("SOAP Fault: (faultcode: {$result-faultcode}, faultstring: {$result-faultstring})", E_USER_ERROR);

}

else

{

$data = $result-GetArticleResult; //这里返回的是类,必须使用-得到元素的值

print_r($data);

}

?

需要注意的一点是,参数是数组外再包一层数组,就是 array( array() )

附SOAP接口的一些参数:

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

POST /SearchService.asmx HTTP/1.1

Host: 202.105.183.61

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: ""

?xml version="1.0" encoding="utf-8"?

soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap=""

soap:Body

GetArticle xmlns=""

param1string/param1

param2string/param2

/GetArticle

/soap:Body

/soap:Envelope

HTTP/1.1 200 OK

Content-Type: text/xml; charset=utf-8

Content-Length: length

?xml version="1.0" encoding="utf-8"?

soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap=""

soap:Body

GetArticleResponse xmlns=""

GetArticleResultstring/GetArticleResult

/GetArticleResponse

/soap:Body

/soap:Envelope

gsoap 调用webservice内存泄漏

gsoap调用webservice出现内存泄漏

程序运行起来后内存一直增长,出现了内存泄漏,经过各模块的测试分析,将泄漏代码出现在这一段。

/*webservice客户端函数,上传本地数据库数据到远程服务器*/

int

sendtowebservice(char

**data_values,int

n_columns,sqlite3*

conn)

{

char

sql[200]="

";

char

*err_msg;

int

res;

struct

soap

*clientsoap

=

soap_new();

soap_cmac

_ns1__sendonemessage

sendmsg;

soap_cmac

_ns1__sendonemessageresponse

sendmsgresponse;

#if

1

soap_init(clientsoap);

sendmsg.grpid=atoi(data_values[0]);

sendmsg.ctime=atoi(data_values[1]);

sendmsg.allencount=atoi(data_values[2]);

sendmsg.alloutcount=atoi(data_values[3]);

printf("sendmsg.grpid=%d;sendmsg.ctime=%d;sendmsg.encount=%d;sendmsg.outcount=%d\n",sendmsg.grpid,sendmsg.ctime,sendmsg.allencount,sendmsg.alloutcount);

sprintf_s(buffer,sizeof(buffer),"sendmsg.grpid=%d;sendmsg.ctime=%d;sendmsg.encount=%d;sendmsg.outcount=%d",sendmsg.grpid,sendmsg.ctime,sendmsg.allencount,sendmsg.alloutcount);

writelog(buffer);

struct

soap_env__header

header;

clientsoap-header=header;

string

strid("admin");

string

strpsw("123456");

soap_cmac

ns1__mysoapheader

mysoapheader;

mysoapheader.userid=strid;

mysoapheader.userpw=strpsw;

header.ns1__mysoapheader_=mysoapheader;

clientsoap-header=header;

if(soap_call___ns1__sendonemessage(clientsoap,

null,

null,

sendmsg,

sendmsgresponse)==soap_ok)

{

//printf("response=%d\n

",

sendmsgresponse.sendonemessageresult);

sprintf_s(buffer,sizeof(buffer),"sendmsgresponse.sendonemessageresult=%d",sendmsgresponse.sendonemessageresult);

writelog(buffer);

/*

-1

=验证失败,

-2=失败,1=插入成功,2=更新成功;

插入成功,更新成功写数据库,将数据标为已发送。

失败,返回-1,上传数据线程释放资源

*/

switch(sendmsgresponse.sendonemessageresult)

{

case

0:

soap_destroy(clientsoap);

soap_end(clientsoap);

soap_done(clientsoap);

return

-1;

break;

case

1:

writelog("上传到服务器,数据插入成功");

sprintf_s(sql,sizeof(sql),"update

grp

set

issend=%d

where

ctime=%d

and

gropid=%d",1,atoi(data_values[1]),atoi(data_values[0]));

res=sqlite3_exec(conn,

sql,

null,

0,

err_msg);

if(res!=sqlite_ok)

{

fprintf(stderr,"操作失败,错误代码:%s",err_msg);

sprintf_s(buffer,sizeof(buffer),"操作失败,错误代码:%s",err_msg);

writelog(buffer);

}

else

{

printf("本地数据issend更新为1成功\n");

writelog("本地数据issend更新为1成功");

}

sqlite3_free(err_msg);

break;

case

2:

writelog("上传到服务器,数据更新成功");

sprintf_s(sql,sizeof(sql),"update

grp

set

issend=%d

where

ctime=%d

and

gropid=%d",1,atoi(data_values[1]),atoi(data_values[0]));

res=sqlite3_exec(conn,

sql,

null,

0,

err_msg);

if(res!=sqlite_ok)

{

fprintf(stderr,"操作失败,错误代码:%s\n",err_msg);

sprintf_s(buffer,sizeof(buffer),"操作失败,错误代码:%s",err_msg);

writelog(buffer);

}

else

{

printf("本地数据issend更新为1成功\n");

writelog("本地数据issend更新为1成功");

}

sqlite3_free(err_msg);

break;

case

-1:

writelog("-1,连接服务器验证失败");

soap_destroy(clientsoap);

soap_end(clientsoap);

soap_done(clientsoap);

return

-1;

break;

case

-2:

writelog("-2,失败");

soap_destroy(clientsoap);

soap_end(clientsoap);

soap_done(clientsoap);

使用浏览器怎么调用Java Web Service的接口

Java通过WSDL文件来调用webservice:

注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。

1,直接AXIS调用远程的web service

这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:

import java.util.Date;

import java.text.DateFormat;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import javax.xml.namespace.QName;

import java.lang.Integer;

import javax.xml.rpc.ParameterMode;

public class caClient {

public static void main(String[] args) {

try {

String endpoint = "";

//直接引用远程的wsdl文件

//以下都是套路

Service service = new Service();

Call call = (Call) service.createCall();

call.setTargetEndpointAddress(endpoint);

call.setOperationName("addUser");//WSDL里面描述的接口名称

call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,

javax.xml.rpc.ParameterMode.IN);//接口的参数

call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型

String temp = "测试人员";

String result = (String)call.invoke(new Object[]{temp});

//给方法传递参数,并且调用方法

System.out.println("result is "+result);

}

catch (Exception e) {

System.err.println(e.toString());

}

}

}

2,直接SOAP调用远程的webservice

这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来

import org.apache.soap.util.xml.*;

import org.apache.soap.*;

import org.apache.soap.rpc.*;

import java.io.*;

import java.net.*;

import java.util.Vector;

public class caService{

public static String getService(String user) {

URL url = null;

try {

url=new URL("");

} catch (MalformedURLException mue) {

return mue.getMessage();

}

// This is the main SOAP object

Call soapCall = new Call();

// Use SOAP encoding

soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

// This is the remote object we're asking for the price

soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");

// This is the name of the method on the above object

soapCall.setMethodName("getUser");

// We need to send the ISBN number as an input parameter to the method

Vector soapParams = new Vector();

// name, type, value, encoding style

Parameter isbnParam = new Parameter("userName", String.class, user, null);

soapParams.addElement(isbnParam);

soapCall.setParams(soapParams);

try {

// Invoke the remote method on the object

Response soapResponse = soapCall.invoke(url,"");

// Check to see if there is an error, return "N/A"

if (soapResponse.generatedFault()) {

Fault fault = soapResponse.getFault();

String f = fault.getFaultString();

return f;

} else {

// read result

Parameter soapResult = soapResponse.getReturnValue ();

// get a string from the result

return soapResult.getValue().toString();

}

} catch (SOAPException se) {

return se.getMessage();

}

}

}

(责任编辑:IT教学网)

更多

推荐计算机等级考试文章