C # under the Webservcie implementation code and in VC and Python under the implementation of the call

Source: Internet
Author: User
Tags goto soap web services xmlns
Web C # Under the Webservcie implementation code, very simple to see what kind of function is completed

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Diagnostics;
Using System.Web;
Using System.Web.Services;

Namespace WebHelloZ5
{
<summary>
Summary description of the Service1.
</summary>
public class Service1:System.Web.Services.WebService
{
Public Service1 ()
{
CodeGen: This call is required by the ASP.net Web service designer
InitializeComponent ();
}

#region Component Designer generated code

Required by the WEB service designer
Private IContainer components = null;

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing && components!= null)
{
Components. Dispose ();
}
Base. Dispose (disposing);
}

#endregion

WEB Services Sample
HelloWorld () sample service return string Hello World
To build, uncomment the following lines, and then save and build your project
To test this WEB service, press the F5 key

[WebMethod]
public string HelloWorld1 ()
//{
Return to "Hello World";
//}

[WebMethod]
public string HelloWorld (int narg, string strarg)
{
return strarg+ narg.tostring ();
}


}
}


Here is the packet that was sent on the network when the webservice was called

Client Request data:

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>
<nArg>int</nArg>
<strArg>string</strArg>
</HelloWorld>
</soap:Body>
</soap:Envelope>

Server Response data:

http/1.1 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>
<HelloWorldResult>string</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>



The automatically generated proxy class under VC7, as follows:

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

The process is:

1 initialization parameter list (setclientstruct (&__params, 0);)
|
V

2. Generate Send Data request (Generateresponse (GetWriteStream ()); SendRequest (_t ("SOAPAction: \" http://tempuri.org/helloworld\ "\ r \ n");)
|
V
3. Receive and parse response data (Beginparse (__atlsoap_spreadstream);)
|
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= "<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 ()

The process is similar to the above if the analysis is implemented. asmx? The WDSL file can be used to add Web references to vs.
The rest is mainly the processing of special symbols and the conversion of types.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.