Create and use WEB Services in C ++

Source: Internet
Author: User

Web services are indeed an exciting part of. net-but they are much larger than. net. The principle is simple. Almost all services that you can name have some mechanisms for executing server-side code: you enter a URL in the address bar of your browser and receive your request, start running something on the server and return the expected result on an html page. It may be ASP, ASP.net, servlets, or even a perl version triggered by CGI five years ago. Therefore, imagine that if the Running code returns results in XML format rather than HTML format, and the service request is not a url entered in the browser address bar, in some code, the GET method in HTTP is used to request the server to obtain the URL address. What you get now is a connection between a program and a program. Coincidentally, each language has a class library, so it is very easy to generate a GET request in HTTP, and there are some shortcuts to parse XML. This solution provides you with a cross-platform, cross-language, cross-vendor, and even all methods, as long as they are connected over the INTERNET or in other ways, we can call the code on another completely different machine in the code of a program.

This is the basic concept behind WEB Services. But how do you pass parameters to a function when using code as a function rather than a page? What if it returns a complex data type? What if a WEB server provides more than one WEB service? How do you find out the names and determine which services are needed? This is the problem described in this article. There are some standards for development using a language similar to the WEB Service Description Language (I .e., wizdle will be cooler) that cover these technical details. If you use Visual Studio. NET to create a WEB service, it meets these standards. If you only need a WEB service, no matter how it is created, using Visual Studio. NET, you will find that borrowing others' code is so simple.

Compile a WEB Service

To write a WEB service, you must use at least one method to write a class. This class must have the WebService attribute, and the method must also have the WebMethod attribute. For example, a category can represent a customer, and there is a way to learn about the customer's shipping information, or to add new items to the customer's shopping list. WEB methods can accept and return any available types, including your defined object instances. They can do anything: maintain internal and external consistency of database data, perform any form of calculation, or even call another WEB method to complete the task. To illustrate this, I will use the Add method to write a CalculatorService class. As you guessed, I will Add two numbers and return their sum.

I created a new project in Visual Studio. NET. In the Visual C ++ Project template, I chose manageable WEB Services. I name the project Calculator. The generated code includes a Class1 class -- although I can select it in the class list and change its name in the attribute window, but the first thing I did was to manually name it. cpp file and. the H file is changed to CalculatorService. (I try to avoid the combination of classes and namespaces, which will confuse intelliisense) no matter how I name it, I must manually calculate the file. asmx for modification. I double-click it in Solution Explorer and modify it. After modification:

      
       <%@ WebService Class=Calculator.CalculatorService %>
      


I got a method called HelloWorld (). It's easy to change it to Add ()-I just changed it. cpp file and. the H file name, changed the signature so that it can accept floating point numbers, and then added some code to return and.

End part of the class declaration:

      
       using <System.Web.Services.dll>using namespace System;using namespace System::Web;using namespace System::Web::Services;namespace Calculator{    public __gc         class CalculatorService : public WebService    {    public:        [System::Web::Services::WebMethod]         double  Add(double x, double y);    };}
      


Implementation:

      
       #include "stdafx.h"#include "Calculator.h"#include "Global.asax.h"namespace Calculator{    double CalculatorService::Add(double x, double y)    {        return x + y;    }}
      


If you are always following me, you can select DEBUG in the "Start" menu to test the code. You have not actually run a WEB service, but this will open a WEB browser and load Calculator. asmx into it. This is a file running on the WEB server that truly applies the classes and methods you define. Open a browser window and enter this URL: http: // localhost/Calculator. asmx. If you encounter any problems, make sure that your WEB server has been started and the browser has not used a proxy server. On the proxy server, localhost is the proxy server, not your machine. You can open the Add link to see the document generated for the method, or even test it by typing a number and clicking the execute button. In this case, another browser window will be opened to display the results that you want to enclose in XML. For example, input 2 and 3 to get the following XML:

      
       <?xml version="1.0" encoding="utf-8" ?> <double xmlns="http://tempuri.org/">5</double>
      


2 plus 3 equals 5, and the WEB service seems to be working.

Use WEB Services

Writing a WEB Service is quite simple: you only need a class attribute, a method attribute, and a calculator. asmx file, all of which are generated by Visual Studio. It will be simpler if only one of them is needed. At the beginning, I created a manageable C ++ program named CalcTest. Before you can use the WEB service, you need to let your project know where to find it. In Solution Explorer, right-click the CalcTest project and choose add WEB parameters. The add WEB parameters dialog box has an editable box for you to enter a URL. You can also search for WEB services using directories on the INTERNET or your own test machine.

The simplest way is to enter the URL to Calculator. asmx and press Enter. You will see the same files as before when running the WEB service project. Click Add parameter to end the process.

Once a parameter is added, calling the WEB Service is like calling any C ++ class. Add parameters to create a header file that can be included when you want to use WEB services. I replaced the line that outputs Hello World with the object that represents my WEB service and the code that uses it. The changed CalcTest. cpp file is as follows:

      
       #include "stdafx.h"#using <mscorlib.dll>#include <tchar.h>#include "WebService.h"using namespace System;// This is the entry point for this applicationint _tmain(void){    CalculatorService * Calc = new CalculatorService;    System::Console::WriteLine("1 plus 1 is {0}",                                __box(Calc->Add(1,1)));    return 0;}
      


(If the keyword does not respond, view my previous articles on the basic types of packing and unpacking)

When the program runs, it outputs nothing different from what I expected:

      
       1 plus 1 is 2
      


It is so easy to call a WEB method from the code. If you want to know what happened in XML, why don't you parse the numbers internally? This is just one of the many conveniences you can make when adding WEB parameters.

The possibility of this point is endless. Any code you run on the server can be accessed by other codes over the INTERNET if you want. Security, authentication, encryption, and others are available to you, and are also supported by SOAP (one of the WEB service standards. Since these files are actually part of ASP. NET, all you know about ASP. NET can also be used in. net web services. Why not try program integration?

About the author

Kate Gregory is one of the creators of Gregory Consulting (www.gregcons.com. In April January 2002, he was assigned the head of the MSDN Toronto Region. Her experience in C ++ can be traced back to the existence of Visual C ++. She is a well-known speaker in universities and Microsoft, and covers NET, Visual Studio, XML, UML, C ++, Java, and Internet. Kate and her colleagues at the company are good at combining software and

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.