ArticleDirectory
- HTTP 1.1
- HTTP 1.2
- HTTP POST
Vc6 ConsoleProgramUse soaptoolkit3.0 to call WebService
1. First install the soaptoolkit3.0 installation package (my installation directory is: D: \ Program Files \ mssoap \)
2. create a VC console Program (empty project), Project name: winconsole6invokewebservice, add a C ++ source file (main. CPP), copy the Lib file D: \ Program Files \ mssoap \ Lib \ mssoap30.lib under the soap installation directory to the project folder.
3. AddSource code:
# Include <stdio. h >#include <iostream >#include <vector> # import "msxml4.dll" using namespace STD; 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; void query (char * endpointurl, char * namespace, char * method, vector <String> & V) {isoapserializerptr serializer; isoapreaderptr reader; isoapconnectorptr connector; // connect to the serviceconnector. createinstance (_ uuidof (httpconnector30); connector-> property ["endpointurl"] = endpointurl; // interface location connector-> connect (); // connect to the server // begin messageconnector-> property ["soapaction"] = _ bstr_t (namespace) + _ bstr_t (method); connector-> beginmessage (); serializer. createinstance (_ UU Idof (soapserializer30); // connect the serializer to the connector input string serializer-> Init (_ variant_t (iunknown *) connector-> inputstream )); // create a SOAP message serializer-> startenvelope ("Soap", "", ""); serializer-> startbody ("body"); serializer-> startelement (method, namespace, "", ""); // The namespace must have for (vector <string >:: iterator it = v. begin (); it! = V. end (); It ++) {serializer-> startelement ("username", namespace, "", ""); serializer-> writestring (IT-> c_str ()); serializer-> endelement ();} serializer-> endelement (); serializer-> endbody (); serializer-> endenvelope (); connector-> endmessage (); // send the message to the Web Service // read the response reader. createinstance (_ uuidof (soapreader30); reader-> load (_ variant_t (iunknown *) connector-> outputstream), ""); printf ("Answer: % s \ n ", (const char *) Reader-> rpcresult-> text); // reader-> rpcresult-> gettext () equivalent} int main (INT argc, char * argv []) {coinitialize (null); char * endpointurl = "http: // 192.168.0.100/webservice1/service. asmx "; char * namespace =" http://tempuri.org/"; vector <string> V1, V2; v2.push _ back (" joeblack "); query (endpointurl, namespace," hello ", v2); couninitialize (); getchar (); Return 0 ;}
In this way, the program is completed and the WebService Service is ready to run.
The WebService is developed using ASP. net2005 (C #). The source code is as follows:
Using system; using system. web; using system. web. services; using system. web. services. protocols; [WebService (namespace = "http://tempuri.org/")] [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] public class service: system. web. services. webService {Public Service () {// If the designed component is used, uncomment the following line // initializecomponent ();} [webmethod (description = "let's say \" Hi \ "")] Public String HI () {return "El Lo world, Happy New Year! ";} [Webmethod (description =" Hello joeblack ")] Public String Hello (string username) {return username +", Happy New Year! ";} [Webmethod (description =" summation method ")] public double addition (double I, Double J) {return I + J ;} [webmethod (description = "")] public double subtract (double I, Double J) {return I-j ;} [webmethod (description = "Product Method")] public double multiply (double I, Double J) {return I * j ;} [webmethod (description = "method of quotient")] public double division (double I, Double J) {If (J! = 0) return I/J; else return 0 ;}}
The method of calling the hello method is as follows:
HTTP 1.1
The following are examples of soap 1.2 requests and responses. ThePlaceholderIt must be replaced with the actual value.
Post/webservice1/service. asmx HTTP/1.1 HOST: localhostcontent-type: text/XML; charset = utf-8Content-Length:LengthSoapaction: "http://tempuri.org/Hello" <? 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> <Hello xmlns = "http://tempuri.org/"> <username>String</Username> </Hello> </soap: Body> </soap: envelope>
HTTP/1.1 200 okcontent-type: text/XML; charset = utf-8Content-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> String</Helloresult>
HTTP 1.2
The following are examples of soap 1.2 requests and responses. ThePlaceholderIt must be replaced with the actual value.
post/webservice1/service. asmx HTTP/1.1 HOST: localhostcontent-type: Application/soap + XML; charset = utf-8Content-Length: length
string
HTTP/1.1 200 okcontent-type: Application/soap + XML; charset = utf-8Content-Length:Length<? XML version = "1.0" encoding = "UTF-8"?> <Soap12: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope"> <soap12: body> String</Helloresult>
HTTP POST
The following are examples of http post requests and responses. ThePlaceholderIt must be replaced with the actual value.
Post/webservice1/service. asmx/Hello HTTP/1.1 HOST: localhostcontent-type: Application/X-WWW-form-urlencodedcontent-length:LengthUsername=String
HTTP/1.1 200 okcontent-type: text/XML; charset = utf-8Content-Length:Length<? XML version = "1.0" encoding = "UTF-8"?> <String xmlns = "http://tempuri.org/">String</String>