Use Visual Studio. NET to call WebService

Source: Internet
Author: User

I. Overview

Many times I have seen netizens discussing WebService calling in VC. In fact, it is quite easy to call WebService in Visual Studio. NET (vs. net) and later versions. All you have to do is "find the WebService publishing address" and add the reference to the VC project. Next, vs. NET will help you generate proxy classes, which will help you deal with a lot of troubles, including network calling, data transmission, and so on. You can ignore soap and networks.

2. hosting or non-hosting?

For the C ++ proxy class generated by vs. net, many people think that managed code must be used. This is not the case. Vs. Net can generate proxy classes for hosted and non-hosted versions. It is your choice if you want to use managed services.
Specifically,. net will generate a proxy class based on the hosted class library; you can find the line of the class in the generated code: Public System: Web: Services: Protocols: soaphttpclientprotocol. For programs that do not use hosting, vs. Net generates Atl-based code. You can find similar lines in the generated code: Template >.
I think, if not, most people will choose an unmanaged method, because it can at least let our program run out of. NET Framework.

3. dynamically set the WebService call address

This is one of the many questions discussed on the Internet, because the address of websercie cannot be static, especially during development and debugging. Fortunately, you can easily set the WebService address at runtime in the two versions of proxy classes. (For details, refer to the instance)

4. program example (unmanaged)

As some articles have already detailed the WebService calling process in the VC hosting program, the following example only describes the WebService calling method in the unmanaged VC program.

1. Create an unmanaged MFC application. Note that it is not hosted, as shown in figure 1)


Figure 1. Creating an unmanaged Program
Check the VC project properties to confirm that the managed service is not used.


Figure 2. Check whether managed service is used

2. Add a WebService reference
On the VC project name, right-click and select "add web reference ". For example (Figure 3 ):


Figure 3. Add a web reference

In the "add web reference" dialog box that appears, enter the websercie reference address and click "go to". The WebService prompt page is displayed.


Figure 4. "add web reference" dialog box

Click "service description" on the page to view the specific webmethod declaration. In my WebService example, only one web method is defined. This method accepts a string as the user name and returns a string as a greeting to the user. As shown in:


Figure 5. view the webmethod prototype

Enter "Web reference name" in, and then click "add reference ". (In the agent category of the unmanaged version, the "Web reference name" entered here does not have any substantive purpose, so you can enter a name as needed. However, in the managed proxy class, the "Web reference name" will become the namespace of the generation class ). Next, vs. NET will generate a WebService proxy class. After it is generated, the WebService. h header file will be automatically opened:

WebService. H is not a proxy class. This header file is actually used to contain all the header files of the proxy class. You can add a few more "web references" to try.

3. Browsing proxy
Let's take a look at the generated proxy classes to have a basic understanding. Switch to "Class View", and you can see a "debug" namespace, expand all, and you can see all the members of the generated proxy class:


Figure 6. Browse generated code

4. Call example
First include the header file and open the namespace

# Include "WebService. H"
Using namespace debug; // This namespace is automatically generated, and is related to the implementation of Web Services.

The following is the call code

Void cinvokedemodlg: onbnclickedbutton1 ()
{
// Todo: add the control notification handler code here

// Because the generated code is based on ATL, You need to initialize com
Coinitialize (null );

Hresult hR = s_ OK;
Ccombstr hiresult;
Ccombstr username = "vckbase ";

Cdebug * DEBUG = new cdebug; // proxy object

// You can call seturl to dynamically set the Web service address
// Debug-> seturl ("http://blog.eray.cn/debug.asmx ");

HR = debug-> Hi (username, & hiresult); // note that the returned value is reversed in the pointer format.

If (failed (HR ))
{
MessageBox ("Call failed ");
}
Else
{
Cstring STR (hiresult );
MessageBox (STR, "Call result ");
}

Delete debug;
Couninitialize ();
}

Because the generated proxy class is based on ATL, you must initialize the com call before calling. In the above Code, ccombstr is used instead of BSTR. Because ccombstr is of the smart type, you can manage the memory allocation by yourself, which is more convenient. In the above Code, a line of annotated code calls seturl to set the WebService call address. In the actual project, you can write this address in the configuration file.
5. Running result
Come on, take a look ~

V. Conclusion

As shown in the preceding example, it is quite simple to call WebService by implementing unmanaged C ++ in vs. net. Of course, in actual use, more code logic, such as error handling, is required for program growth.

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.