The SVC6 console program uses SoapToolkit3.0 to invoke WebService

Source: Internet
Author: User
Tags soap

1. First install the SOAPTOOLKIT3.0 installation package and install it (my installation directory is: C:\Program files\common Files)

2. New VC Console Program (empty project), project name: Winconsole6invokewebservice, adding a C + + source file (main.cpp)

3. Main.cpp Source 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 Service    connector.createinstance (__uuidof (HTTPCONNECTOR30));     connector->property["Endpointurl"] = endpointurl;       //Interface location &nBsp;   connector->connect ();                                     //and server connection          // Begin message    connector->property["soapaction"] = _bstr_t (Namespace) + _bstr_t (method);     connector->beginmessage ();      serializer.createinstance (_ _uuidof (SOAPSERIALIZER30))      //Connect serializer to connector input string      serializer->init (_variant_t ((iunknown*) connector->inputstream));      // Create a SOAP message     serializer->startenvelope ("Soap", "", "");     serializer- >startbody ("body");     serializer->startelement (method, Namespace, "", ""); namespaces must have &NBSP;&NBSP;&NBsp;  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 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;}

  

3> Publish the service through VS2010, add webservices, file name Service.asmx, cannot be published through WCF, otherwise the above code returns wrong.

Service.asmx File Source

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 using a design component, uncomment the following line//initializecomponent (); } [WebMethod (Description = "Let's say \" Hi\ "")] public string Hi () {return "Hello World, Happy New year!    ";  } [WebMethod (Description = "Hello Joeblack")] public string Hello (string username) {return username + ",     Happy New year! ";}    [WebMethod (Description = "method of summation")] public double addition (double I, double j) {return i + J;    } [WebMethod (Description = "Method of Difference")] public double subtract (double I, double j) {return i-j;    } [WebMethod (Description = "method of Quadrature")] public double multiply (double I, double j) {return I * j; } [WebMethod (Description = "operator'sMethod ")] public double division (double I, double j) {if (J! = 0) return i/j;    else return 0; }}

  

The SVC6 console program uses SoapToolkit3.0 to invoke WebService

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.