Webservcie under C # implementation code and call implementation under VC and Python (original)

xiaoxiao2021-03-06  75

The webservcie under the C # implements code, very simple to see what kind of function is done.

Using system; using system.componentmodel; using system.data; using system.web; using system.web.services;

A summary description of Namespace WebHelloz5 {///

/// service1. /// public class service1: system.Web.Services.Webservice {public service1 () {// codegen: This call is the initializeComponent () () necessary for the ASP.NET Web service designer.

#Region Component Designer Generated Code // Web Server Designer The private icontainer component = null; ///

/// designer supports the required method - Do not modify // this method using the code editor content. /// private void initializecomponent ()}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (Disposing && Components! = NULL) {components.dispose ();} Base.dispose (Disposing);} #ENDREGION

// Web Service Sample // HelloWorld () Sample Service Returns Hello World // To generate, cancel the following list, then save and generate items // To test this web service, press F5 button

// [WebMethod] // public string helloworld1 () // {// return "Hello World"; //}

[WebMethod] Public String HelloWorld (int NARG, STRING STRARG) {Return Strarg Narg.toString ();

}

Below is called WebService, the packet sent on the network.

Client request data:

Post /Webhelloz5/service1.asmx http / 1.1 host: localhost content-type: text / xml content-length: length soapaction: "http://tempuri.org/helloworld"

int string Server response data:

HTTP / 1.1 200 OK Content-Type: Text / XML; Charset = UTF-8 Content-Length: Length

String

Automatically generated proxy class under VC7, as shown below:

template inline HRESULT CService1T :: HelloWorld (int nArg, BSTR strArg, BSTR * HelloWorldResult) {HRESULT __atlsoap_hr = InitializeSOAP (NULL); if (FAILED (__ atlsoap_hr)) {SetClientError (SOAPCLIENT_INITIALIZE_ERROR); return __atlsoap_hr; } Cleanupclient ();

CComptruap_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;}

The process is:

1 Initialization parameter list (setClientStruct (& __ params, 0);) | V

2. Generate Send Data Request (GetWritestReam ()); SendRequest (_T ("soapaction: /"http://tempuri.org/helloworld/"/r/N"));) | v 3. Receive and resolution Response data (BeginParse);) | V 4. Clean up work

Python code:

#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 = " "% (strfunc, strxmlns) for (k, v) in Dictarg.Items (): if k is int: Ret =" <% s >% s % (k, str (v), k) else: RET = "<% s>% s "% (k, v, k) RET = " "% (strfunc) RET =" "RET =" "Return Ret Def Hello_World (self, argl): func =" helloworld "addr = urlparse.URLPARSE (Self .ur) argd = {} Argd ["NARG"] = argl [0] argd ["strarg"] = argl [1] tryer = {} header ['Host'] = 'localhost' header ['content TYPE '] =' Text / x ML '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 = ConnML = DataXML =

转载请注明原文地址:https://www.9cbs.com/read-93157.html

New Post(0)