欢迎光临
我们一直在努力

C#下的webservcie 实现代码和 在vc和python下的调用实现-.NET教程,C#语言

建站超值云服务器,限时71元/月

c#下的webservcie 实现代码,很简单一看就清楚了是完成什么样的功能了

using system;

using system.collections;

using system.componentmodel;

using system.data;

using system.diagnostics;

using system.web;

using system.web.services;

namespace webhelloz5

{

/// <summary>

/// service1 的摘要说明。

/// </summary>

public class service1 : system.web.services.webservice

{

public service1()

{

//codegen:该调用是 asp.net web 服务设计器所必需的

initializecomponent();

}

#region component designer generated code

//web 服务设计器所必需的

private icontainer components = null;

/// <summary>

/// 设计器支持所需的方法 – 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void initializecomponent()

{

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void dispose( bool disposing )

{

if(disposing && components != null)

{

components.dispose();

}

base.dispose(disposing);

}

#endregion

// web 服务示例

// helloworld() 示例服务返回字符串 hello world

// 若要生成,请取消注释下列行,然后保存并生成项目

// 若要测试此 web 服务,请按 f5 键

//[webmethod]

//public string helloworld1()

//{

// return "hello world";

//}

[webmethod]

public string helloworld(int narg, string strarg)

{

return strarg+ narg.tostring();

}

}

}

下面就是调用webservice时,网络上大家发送的数据包了

client请求数据:

post /webhelloz5/service1.asmx http/1.1

host: localhost

content-type: text/xml

content-length: length

soapaction: "http://tempuri.org/helloworld"

<?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/">

<narg>int</narg>

<strarg>string</strarg>

</helloworld>

</soap:body>

</soap:envelope>

server回应数据:

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="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>

<helloworldresponse xmlns="http://tempuri.org/">

<helloworldresult>string</helloworldresult>

</helloworldresponse>

</soap:body>

</soap:envelope>

vc7下的自动生成的代理类,如下所示:

template <typename tclient>

inline hresult cservice1t<tclient>::helloworld(

int narg,

bstr strarg,

bstr* helloworldresult

)

{

hresult __atlsoap_hr = initializesoap(null);

if (failed(__atlsoap_hr))

{

setclienterror(soapclient_initialize_error);

return __atlsoap_hr;

}

cleanupclient();

ccomptr<istream> __atlsoap_spreadstream;

__cservice1_helloworld_struct __params;

memset(&__params, 0x00, sizeof(__params));

__params.narg = narg;

__params.strarg = strarg;

__atlsoap_hr = setclientstruct(&__params, 0);

if (failed(__atlsoap_hr))

{

setclienterror(soapclient_outofmemory);

goto __skip_cleanup;

}

__atlsoap_hr = generateresponse(getwritestream());

if (failed(__atlsoap_hr))

{

setclienterror(soapclient_generate_error);

goto __skip_cleanup;

}

__atlsoap_hr = sendrequest(_t("soapaction: \"http://tempuri.org/helloworld\"\r\n"));

if (failed(__atlsoap_hr))

{

goto __skip_cleanup;

}

__atlsoap_hr = getreadstream(&__atlsoap_spreadstream);

if (failed(__atlsoap_hr))

{

setclienterror(soapclient_read_error);

goto __skip_cleanup;

}

// cleanup any in/out-params and out-headers from previous calls

cleanup();

__atlsoap_hr = beginparse(__atlsoap_spreadstream);

if (failed(__atlsoap_hr))

{

setclienterror(soapclient_parse_error);

goto __cleanup;

}

*helloworldresult = __params.helloworldresult;

goto __skip_cleanup;

__cleanup:

cleanup();

__skip_cleanup:

resetclientstate(true);

memset(&__params, 0x00, sizeof(__params));

return __atlsoap_hr;

}

流程为:

1 初始化参数列表( setclientstruct(&__params, 0);)

|

v

2.生成发送数据请求(generateresponse(getwritestream());sendrequest(_t("soapaction: \"http://tempuri.org/helloworld\"\r\n"));)

|

v

3.接收和解析回应数据(beginparse(__atlsoap_spreadstream);)

|

v

4.清理工作

python代码:

#author:zfive5(zhaozidong)

#email: zfive5@yahoo.com.cn

import httplib

import xml.parsers.expat

import urlparse

class zfive5web:

def __init__(self, url,xmlns):

self.url=url

self.xmlns=xmlns

self.ret=""

self.data=""

def gen_request(self,strfunc,strxmlns,dictarg):

ret="<soap:envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">"

ret+="<soap:body>"

ret+="<%s xmlns=\"%s/\">"%(strfunc,strxmlns)

for (k,v) in dictarg.items():

if k is int:

ret+="<%s>%s</%s>"%(k,str(v),k)

else:

ret+="<%s>%s</%s>"%(k,v,k)

ret+="</%s>"%(strfunc)

ret+="</soap:body>"

ret+="</soap:envelope>"

return ret

def hello_world(self,argl):

func="helloworld"

addr=urlparse.urlparse(self.url)

argd={}

argd["narg"]=argl[0]

argd["strarg"]=argl[1]

try:

header={}

header[host]=localhost

header[content-type]=text/xml

header[soapaction]=\"%s/%s\"%(self.xmlns,func)

conn=httplib.httpconnection(addr[1])

print self.gen_request(func,self.xmlns,argd)

conn.request(post,/webhelloz5/service1.asmx,self.gen_request(func,self.xmlns,argd),header)

resp=conn.getresponse()

dataxml=resp.read()

def start_element(name, attrs):

pass

def end_element(name):

if name==helloworldresult:

self.ret=self.data

#elif name==ourputarg:

# argl[0]=self.temp

def char_data(data):

self.data=data

pxml=xml.parsers.expat.parsercreate()

pxml.startelementhandler = start_element

pxml.endelementhandler = end_element

pxml.characterdatahandler = char_data

pxml.parse(dataxml, 1)

except:

return none

return self.ret

def test():

a=zfive5web("http://127.0.0.1/webhelloz5/service1.asmx","http://tempuri.org")

l=[1,121]

ret=a.hello_world([1,121])

if __name__ == __main__:

assert test()

流程与上差不多如果实现分析.asmx?wdsl文件就完全可以vs中的添加web引用的功能,这里

剩下的主要是特殊符号的处理和类型转化工作。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#下的webservcie 实现代码和 在vc和python下的调用实现-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章