首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 操作系统-> FreeBSD教程
C#下的webservcie 实现代码和 在vc和python下的调用实现-.NET教程,C#语言
作者:网友供稿 点击:211
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
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引用的功能,这里
剩下的主要是特殊符号的处理和类型转化工作。



文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Tomcat 5.5 配置 MySQL 数据库连接池-JSP教程,数据库相关
·Remoting编程知识二-.NET教程,.NET Framework
·java 实现web 登陆-JSP教程,Java技巧及代码
·Visual C#中的多线程编程-.NET教程,C#语言
·如何在freebsd4.9平台上安装darwin streaming server 5.0
·从网页上读取源代码,并写入文件-.NET教程,面向对象编程
·小型局域网布线攻略
·在Jsp程序读取或向DB写入数据乱码解决办法-JSP教程,数据库相关
·C#下的webservcie 实现代码和 在vc和python下的调用实现-.NET教程,C#语言
·cable modem技术综述
最新文章
·网站赚钱的四个技巧_网赚技巧
·做一个职业的酋长-访echo_站长访谈
·绿野户外网负责人海光:带着困惑上路_站长访谈
·站长自习室访谈系列:晴空--漫漫八年网络路_站长访谈
·搜房网无线主管王峰:互联网生存四原则_站长访谈
·discuz!6.0新版本尝鲜—“电子商务”功能强大而完善,会员在论坛开店轻而易举_discuz!论坛
·互联网时代的个人站长更需要独立思考_站长心得
·如何让网民爱上你的网站?(2)_站长心得
·运营论坛赢在创意,氛围决定成功_站长心得
·五年以后个人网站像传呼机一样成为历史?_站长心得
相关主题
西部数码虚拟主机

友情链接
CNNIC 西部数码
万网 自助建站
虚拟主机 asp空间
域名注册 域名
域名申请 主页空间
论坛空间 网站空间
国际域名 虚拟空间
空间租用 DDOS防火墙
成都主机托管 四川主机托管
主机租用 服务器租用
网站目录 自助建站
虚拟主机 网址大全
软件下载
自助链接
虚拟主机资讯 特价虚拟主机
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
发表评论 打印  刷新     关闭