WebService calling in VB6.0

Source: Internet
Author: User

Problem description:
In WebService calling with a non-. Net client, it is easy to follow the instructions in using the soap toolkit, but a problem is found during actual use.
Assume that WebService provides the int simplemath. add (INT N1, int N2), the return value is N1 + N2, but according to the example provided by the soap toolkit, the return value is 0 When VC is used for calling.

Record my solution process and forget it.

Test environment:
OS: Windows XP Professional
WebService: vs.. NET 2003
WebService runtime environment: IIS
Client: VC in vc6.0, vs. net
Soaptoolkit SDK: 3.0

Problem reproduction:
See the sample code after a slight modification in Toolkit:

# Include <stdio. h>
# Import "msxml4.dll"
Using namespace msxml2;
# Import "C:/program files/common files/mssoap/binaries/mssoap30.dll "/
Exclude ("istream", "ierrorinfo", "isequentialstream", "_ large_integer ",/
"_ Ularge_integer", "tagstatstg", "_ filetime ")
Using namespace mssoaplib30;

Int Test2 ()
{

Isoapserializerptr serializer;
Isoapreaderptr reader;
Isoapconnectorptr connector;
 
// Connect to the service
Connector. createinstance (_ uuidof (httpconnector ));
 

Connector-> property ["endpointurl"] = "http: // localhost/wstest/simplemath. asmx? WSDL ";
Connector-> connect ();
 
// Begin message
Connector-> property ["soapaction"] = "http://tempuri.org/Add ";
Connector-> beginmessage ();
 
// Create the soapserializer
Serializer. createinstance (_ uuidof (soapserializer ));
 
// Connect the serializer to the input stream of the connector
Serializer-> Init (_ variant_t (iunknown *) connector-> inputstream ));
 
// Build the SOAP message
Serializer-> startenvelope ("","","");
Serializer-> startbody ("");
Serializer-> startelement ("add", "http://tempuri.org /","","");
Serializer-> startelement ("N1 ","","","");
Serializer-> writestring ("5 ");
Serializer-> endelement ();
Serializer-> startelement ("N2 ","","","");
Serializer-> writestring ("10 ");
Serializer-> endelement ();
Serializer-> endelement ();
Serializer-> endbody ();
Serializer-> endenvelope ();
 
// Send the message to the Web Service
Connector-> endmessage ();
 
// Let us read the response
Reader. createinstance (_ uuidof (soapreader ));
 
// Connect the reader to the output stream of the connector
Reader-> load (_ variant_t (iunknown *) connector-> outputstream ),"");
 
// Display the result
Printf ("Answer: % s/n", (const char *) Reader-> rpcresult-> text );
Return 0;
}

The returned result is 15, but 0 is actually returned.
On the Internet, I found someone suggested changing the WebService method parameter to string, and then converting it within the method. I think this is not a good method. As a matter of fact, I found that there will be more problems with this implementation.

The returned result is 15, but 0 is actually returned.
On the Internet, I found someone suggested changing the WebService method parameter to string, and then converting it within the method. I think this is not a good method. As a matter of fact, I found that there will be more problems with this implementation.
Analysis process:

Use the tracking tool mssoapt of soap toolkit to check what data the client sends to WebService:

<SOAP-ENV: envelope SOAP-ENV: encodingstyle = "" xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV: Body SOAP-ENV: encodingstyle = "">
<Soapsdk1: Add xmlns: soapsdk1 = "http://tempuri.org/" SOAP-ENV: encodingstyle = "">
<N1 SOAP-ENV: encodingstyle = ""> 5 </N1>
<N2 SOAP-ENV: encodingstyle = ""> 10 </N2>
</Soapsdk1: Add>
SOAP-ENV: Body>
SOAP-ENV: envelope>

Let's take a look at the data (Template) sent by IE browser in vs. Net debugging ):

<? XML version = "1.0" encoding = "UTF-8"?>
<Soap: envelope xmlns: xsi = "The http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "The http://www.w3.org/2001/XMLSchema" xmlns: Soap = "The http://schemas.xmlsoap.org/soap/envelope/">
<Soap: Body>
<Add xmlns = "http://tempuri.org/">
<N1> int </N1>
<N2> int </N2>
</Add>
</Soap: Body>
</Soap: envelope>

What is the difference? There are multiple encodingstyle attributes in soaptoolkit data, although no value is specified. We try to block this attribute.
The parameter in the startelement method of soapserializer30 is called as follows. You can leave this attribute unspecified.
The Code is as follows:

...
Serializer-> startelement ("add", "http://tempuri.org/", "NONE ","");
Serializer-> startelement ("N1", "", "NONE ","");
Serializer-> writestring ("5 ");
Serializer-> endelement ();
Serializer-> startelement ("N2", "", "NONE ","");
Serializer-> writestring ("10 ");
Serializer-> endelement ();
Serializer-> endelement ();
...

But the returned result is still 0. It seems that it has nothing to do with encodingstyle. Check the tracing information:

<SOAP-ENV: envelope SOAP-ENV: encodingstyle = "" xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV: Body SOAP-ENV: encodingstyle = "">
<Soapsdk1: Add xmlns: soapsdk1 = "http://tempuri.org/">
<N1> 5 </N1>
<N2> 10 </N2>
</Soapsdk1: Add>
SOAP-ENV: Body>
SOAP-ENV: envelope>

Solution:
What is the difference between the requests sent in vs.net? Take a closer look at the fact that no namespace is specified for N1 and N2.
Change the code:

Serializer-> startelement ("add", "http://tempuri.org/", "NONE ","");
Serializer-> startelement ("N1", "http://tempuri.org/", "NONE ","");
Serializer-> writestring ("5 ");
Serializer-> endelement ();
Serializer-> startelement ("N2", "http://tempuri.org/", "NONE ","");
Serializer-> writestring ("10 ");
Serializer-> endelement ();
Serializer-> endelement ();

The test result is normal and 15 is returned.

Look at the XML data sent at this time:
<SOAP-ENV: envelope SOAP-ENV: encodingstyle = "" xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV: Body SOAP-ENV: encodingstyle = "">
<Soapsdk1: Add xmlns: soapsdk1 = "http://tempuri.org/">
<Soapsdk1: N1> 5 </soapsdk1: N1>
<Soapsdk1: N2> 10 </soapsdk1: N2>
</Soapsdk1: Add>
SOAP-ENV: Body>
SOAP-ENV: envelope>

Cause:

I cannot summarize who the problem is :(

Maybe. net problems may be soaptoolkit problems, maybe the examples provided by him have problems themselves, maybe there is a problem with my runtime environment, of course, even more, I have not really understood the XML namespace or used WebService correctly.

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.