麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Python > 正文

C#下的webservcie 實現代碼和 在vc和python下的調用實現

2024-07-21 02:18:37
字體:
來源:轉載
供稿:網友
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: [email protected]

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引用的功能,這里
剩下的主要是特殊符號的處理和類型轉化工作。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中文字幕天堂在线 | 嗯~啊~弄嗯~啊h高潮视频 | 色婷婷久久一区二区 | av在线播放免费 | 在线成人精品视频 | 强伦女教师视频 | 看中国一级毛片 | 舌头伸进添的我好爽高潮网站 | 久久精品中文字幕一区二区 | 中文在线观看视频 | 黄色av网 | 久久精品污| 羞羞色在线观看 | 全黄裸片武则天一级第4季 偿还电影免费看 | 精品亚洲网站 | 72pao成人国产永久免费视频 | 久久99精品久久久久久久久久久久 | 欧美亚洲啪啪 | 伊人二本二区 | 欧美一级毛片特黄黄 | 精品成人av一区二区在线播放 | 久久久资源网 | 中文字幕一区在线观看视频 | 中文字幕视频在线播放 | 羞羞视频在线免费 | 国产一区精品视频 | 日本成人二区 | 毛片免费看电影 | 在线亚州| 日韩在线播放一区二区 | 毛片视频网站在线观看 | 欧美一级性 | 久久视频精品 | 欧美日韩在线视频一区 | 视屏一区| 91网站在线观看视频 | 91精品国产91久久久久久不卞 | 亚洲综合视频网 | 亚洲va久久久噜噜噜久久男同 | 日韩视频在线一区二区三区 | 久成人 |