Call Web Service in VC (unmanaged Mode)

Source: Internet
Author: User
Tags soap
Document directory
  • This is an unmanaged way to use web servie. Don't mention that I didn't remind you that if you use. Net WebService, you can use it like this. If you use ATL, you need to use the published ATL WebService. Of course, I also encountered it during practice. The specific reasons are being analyzed.
This is an unmanaged way to use web servie. Don't mention that I didn't remind you that if you use. Net WebService, you can use it like this. If you use ATL, you need to use the published ATL WebService. Of course, I also encountered it during practice. The specific reasons are being analyzed.

1. Use Visual Studio. NET to call WebService

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 calls, 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
<Typename tclient = csoapsocketclientt <>. 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 to the VC project name. Right-click the project name 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 provide substantial protection, so you can enter a name at will. 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. Browse the proxy class we may wish to look at the generated proxy class 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. The call example first contains the header file and opens 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 comcoinitialize (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 the 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. Check the running result ~

1. Use Visual Studio. NET to call WebService

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 calls, 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
<Typename tclient = csoapsocketclientt <>. 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 to the VC project name. Right-click the project name 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 provide substantial protection, so you can enter a name at will. 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. Browse the proxy class we may wish to look at the generated proxy class 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. The call example first contains the header file and opens 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 comcoinitialize (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 the 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. Check the running result ~

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.