Calling a Web Service in. NET is actually as simple as creating a web service. (CompleteCodeLocation: Optical Disc \ code \ ch07 \
Webappclient)
The implementation steps are as follows.
(1) first, create an ASP. NET web application.Program.
(2) Add a web service reference.
Right-click the project and choose "add web reference" from the shortcut menu, as shown in 7-6. The "add web reference" dialog box, as shown in 7-7 is displayed.
|
Figure 7-6 select the "add web reference" command |
|
(Click to view the larger image) Figure 7-7 "Add web reference" dialog box |
Adding a reference is divided into the following three cases.
If it is a Web service in this solution, select the first one. The program automatically redirects to the service address in the solution.
If it is a Web service on the computer, the system will automatically find the Web service on the local machine for you to choose from.
In either case, the final goal is to input the corresponding address in the URL for loading. Therefore, we can enter the address of the web service in the URL, both local and public networks are the same.
Then, click "go" to list the Web service test page and method description, and verify whether the project can use the web service, as shown in 7-8.
|
(Click to view the larger image) Figure 7-8 preview web reference |
In this case, enter a name in the "Web reference name" text box and use this name in the code to access the selected web service programmatically, click Add reference.
(3) In this case, a directory named web references is added to the project, as shown in 7-9. The automatically generated proxy class is placed here.
(4) Click the "show all files" button in Solution Explorer of the Project. Then you can see the proxy class named reference. CS.
(5) using this proxy class in code is basically the same as using a local common class.
- Productservice. ltpservice service =NewProductservice. ltpservice ();
- StringPrice = service. getproductprice ("001");
So far, ASP. NET has finished calling Web service, and only two lines of code have been written.