Creating and using Web Services in C + +

Source: Internet
Author: User
Tags html page include web services visual studio

Web services are really exciting parts of. NET-but they are bigger than. Net. The truth is simple. Almost every service you can name has a mechanism for executing server-side code: You enter a URL in the browser's address bar, receive your request, start running something on the server, and then return the results you want with the HTML page. It could be asp,asp.net,servlets, or even Perl, which was triggered by CGI five years ago. So imagine if the running code returns the results in XML format instead of HTML format, and the service request is not a URL entered in the browser's address bar, but rather some code that asks the server for a URL address with a Get method in HTTP. So what you're getting is a connection between the program and the program. It happens that every language has a class library, so it's easy to generate a GET request in HTTP, and there are shortcuts to parsing XML. This scenario provides you with a cross-platform, cross-language, vendor-and-everything approach, so long as they are connected to the Internet or otherwise, we can invoke code from a different machine in the code of a program.

This is the basic idea behind Web services. But as a function rather than a page, how do you pass arguments to a function? What if it returns a complex data type? What if more than one Web service is available on a Web server? How do you find those names and confirm what kind of service you need? This is the problem to be explained in this article. Using a Web Service Description language (which says Wizdle will be cooler) has certain criteria that cover these technical details. If you create a Web service with Visual Studio.NET, it will meet these criteria. If you just need a Web service, regardless of how it was created, through Visual Studio.NET, you'll find that the code for borrowing someone else's is so simple.

Writing a Web Service

In order to write a Web service, you should at least write a class in one way. This class must have a WebService attribute, and the method should have a WebMethod property. For example, a class can represent a customer and have 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 type, including the object instance you define. They can do anything: maintain the internal and external consistency of database data, do any form of operation, or even invoke another Web method to accomplish the task. To illustrate this point, I'll use the Add method to write a CalculatorService class that, as you can guess, add up to two numbers and return to their and.

I created a new project in Visual Studio.NET. In the Visual C + + engineering template, I chose a manageable Web service. I named the project calculator. The resulting code includes a CLASS1 class--although I can select it in the list of classes and always change its name in the Properties window, the first thing I do is manually change the name of the. cpp file and the. h file to the CalculatorService. (I try to avoid the names of classes and namespaces that will confuse IntelliSense) no matter how I name it, I have to manually make changes to the file calculator.asmx, I double-click it in Solution Explorer and then modify it. After the modification:

<@ WebService Class=Calculator.CalculatorService %>

I got a method called HelloWorld () that was simple--I just changed the name of the. cpp file and the. h file, changed the signature so that it could accept the floating-point number, and then added some code to return and.

End part of 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);
 };
}

Part of the 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 have been following me, you will be able to choose Debug from the Start menu to test the code. You are not actually running a Web service, but this will open a Web browser and load the calculator.asmx in. This is a file that is actually running on a Web server using your defined classes and methods, and if you like, open a browser window and enter this url:http://localhost/calculator/calculator.asmx. If you encounter any problems, make sure that your Web server is started and that the browser does not use 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, and even test it by typing the number and clicking the Execute button. At this point, you will open another browser window to show the results you have requested to surround in XML. For example, I enter 2 and 3, and I get the following xml:

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

2 plus 3 equals 5,web service seems to be working.

Using Web Services

Writing a Web service is fairly straightforward: you only need a class attribute, a method attribute, and a calculator.asmx file, all three of which are generated by Visual Studio. If only one of them would be simpler. At the beginning I set up a manageable C + + program called Calctest to. Before you can use a Web service, you need to let your project know where to find it. I right-click the Calctest project in Solution Explorer, and then select Add Web Parameters. The dialog box for adding Web parameters has an editable box for you to enter the URL. You can also use directories to find Web services on the Internet or on your own test machines.

The easiest way to do this is to type the URL to calculator.asmx and then press ENTER. You will see the same files that you used to run the Web services project. Click Add parameter to end this process.

Once the parameter is added, calling the Web service is like calling any C + + class. Add parameters to create a header file that can be included in any Web service you want to use. I've replaced the line for Hello World with the creation of the object that represents my Web service and the code that uses it. The changed CalcTest.cpp file is like this:

#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
#include "WebService.h"
using namespace System;
// This is the entry point for this application
int _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, see my previous article on the basic type of boxing and unpacking)

When the program runs, it outputs, and I expect it to be not bad:

1 plus 1 is 2

Calling a Web method from your code is as simple as that. If you want to know what's going on in XML, why don't you need to parse the numbers from the inside? This is just one of the many conveniences you'll be given when you add Web parameters.

The possibilities for this point are endless. Any code you run on the server, if you want, can be accessed by other code over the Internet. Security, authentication, encryption, and everything else are available to you, and are also supported by soap (one of the Web service standards). Since those files are actually part of the asp.net, all you know about asp.net can also be applied to Web services in. Net. Now why not try to integrate the program into the program?

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.