ASP. NET method for calling WebService service

  • 2021-07-22 09:35:02
  • OfStack

This article illustrates how ASP. NET invokes the WebService service. Share it for your reference, as follows:

1. WebService: WebService is an application logic unit that can be accessed by programs in a platform-independent manner through the standard Web protocol.

(1) Application Logic Units: The web service includes 1 application logic units or code. These codes can complete computing tasks, database queries, and any work that computer programs can do.

(2) Program Accessibility: Currently, most web sites are accessed manually through browsers, while web services can be accessed by computer programs.

(3) Standard we protocol: All protocols of Web service are based on a group of standard Web protocols, such as HTTP, XML, SOAP, WSDl, UDDI and so on

(4) Platform independence: web services can be implemented on any platform. Because the standard protocol is not dedicated to a single vendor, it is supported by most major vendors.

SOAP (Simple Object Access Protocol, Simple Object Access Protocol) protocol: It is a lightweight protocol for exchanging XML encoded information, and SOAP protocol is a combination of HTTP and XML protocols.

WSDL (Web Service Description Language, Web service description language) is the standard of XML document to describe Web service, and is the interface definition language of Web service.

2. Create the ASP. NET WEB service

Establishing an Web service is to expose information or logic to other computers and customers. Further, it is to establish one or more methods from classes that support SOAP communication.

To create an Web service: (VS2008)

First, create a new website. Select Files---- > New--- > Website, open the "New Website" dialog box and select the "ASP. NET web Service" template. After confirmation, the creation was successful.

When created, Service. asmx is generated with the code-behind file Servece. cs in the App_Code file. Web service files all use the extension. asmx.

It is found that in VS 2010, the web service is created by creating a new website, selecting Add New Item, and then selecting web service.

3. Use the ASP. NET Web service

(1) Add an Web reference

1. Create a new website, right-click the root directory of the website, and select the item "Add Web Reference".

2. In the "Add Web Reference" dialog box, select or fill in the URL of the Web service in the URl list box. (. asmx file path) Then fill in the Add Web Reference text box with the Add Web reference name you want to use in your application.

3. Click the "Add Reference" button, which is in the App_WebReferences directory, where the WSDL file for the Web service has been added. In the application's web. config file < appSettings > Block, the following configuration is added to set the actual Web reference.

(2) The client calls the Web service

After the Web reference service is added, the Web service can be invoked on the client side.

1. Add default. aspx to the newly created website, and add 1 TextBox control, 1 Button control and 1 Label control to the page

2. The click event code for adding the button "Call Serice service" is as follows.


Using SimpleWeb;
protected void Button1_Click(object sender,EventArgs e)
{
// Instantiation Service  Object 
Service ms=new Service();
// Call Service Object ms Adj. HelloWord Method 
// Will TextBox.Text As a parameter to the HelloWord Method 
Label1.Text=ms.HelloWorld(TextBox1.Text);
}

(3) Browsing the web page Default. aspx, entering a name in the browser, and clicking the button will call the HelloWorld method of the web service Service and display the result on Label.

Summary: The whole process of applying Web service is: adding Web service----- > Add an Web reference------ > Client call

For more readers interested in asp. net, please check the topics on this site: "Summary of String Operation Skills of asp. net", "Summary of Operation Skills of XML of asp. net", "Summary of File Operation Skills of asp. net", "Summary of Skills of asp. net ajax" and "Summary of Cache Operation Skills of asp. net".

I hope this article is helpful to everyone's asp. net programming.


Related articles: