How to create and use Web services [Repost]

xiaoxiao2021-03-06  55

How to create and use Web services

Author: Meng will come from: [Meng] will be the wonderful world ----------------------------------- ---------------------------------------------

Introduction

One of the most powerful aspects of .NET is that it can use it to create a web service. A web service is an external interface that is available for other websites provided by a website. For example, a financial company can provide detailed stock quotes with its trading partners through the web service, which can be read and displayed through the web page, or it is also available from the customer's desktop computer. Read it in the program.

This article describes two aspects of Web services: One is how to create a web service; the other is how to use Web services. As an example, we explain how to create a web service in a Web service from Aspfaqs.com (http://www.aspfaqs.com/aspfaqs/).

Create a web service

Before you create a web service, you must first ask yourself: "What services I want to provide to my users?" The goal of this article is to create such a web service: let other users show a list of common problem solutions from ASPFAQs.com on their own websites. The ideal feature is to limit other websites to view FAQs classification and FAQs arranged by classification. If you want to view a question, let users go to access the website http://www.aspfaqs.com. The web service of this article will eventually provide the following features to other websites:

1. Browse list of all FAQ categories 2, browse all FAQs in a category. 3. Browse a problem with a FAQ, but does not include the answer. Creating a web service is very simple, first create a .asmx file (you can use Visual Studio .NET or any text editor you like, it is recommended to use Web Matrix, it has a template for creating a web service), web service An ordinary class creates, with a macro in front of the method, indicating that this method is accessed through a web service.

For Aspfaqs.com's web services, first create three methods, getcategories, getfaqsincategory, and getfaq, and GetFaq, respectively, and GetFAQ, respectively, and GetFaq, respectively. And create a private method getDataSet, press the SQL query group to be loaded into a DataSet. Below is the code implemented:

<% @ WebService Language = "VB" Class = "ASPFAQs"%> Imports System.Web.Services Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Public Class ASPFAQs' create a private function method GetDataSet Private Function GetDataSet (strSQL as String) as DataSet '1. Create a connection object SqlConnection Dim myConnection as New SqlConnection (ConnectionString)' 2. Create COMMAND objects pass SQL parameter Dim myCommand as New SqlCommand (strSQL, myConnection) myConnection.Open () '3. Create DataAdapter object Dim myDataAdapter as New SqlDataAdapter () myDataAdapter.SelectCommand = myCommand '4. generating DataSet and closes the connection Dim myDataSet as New DataSet () myDataAdapter.Fill (myDataSet) myConnection.Close ()' returns DataSet return myDataSet End Function 'Create Implement three tasks public function getcategories () AS Dataset Return getDataSet (SQL Query) 'Here the parameter SQL is used to get all FAQ classification End function public function getfaqsincategory (Catid AS Integer AS Dataset Return Get DataSet (SQL Query) 'Here the parameter SQL is used to get all faq end function public function getFAQ (FAQID AS Integer), the parameter SQL here To get a FAQ (FAQID) End Function End Class As mentioned earlier, three methods accessed through the web service have a preamble, and there is a @Webservice indicate the language used in the .asmx file. And class names. This web service is named ASPFAQS, and the same name. Once you have created the .asmx file, save it to the directory that can be accessed through the web, then you can access it through a web browser. For example, if the web service file name is aspfaqs.asmx, save to / WS directory, you can see the web service via http://aspnet.4guysfromrolla.com/ws/aspfaqs.asmx All public methods used, you can also provide input parameters to see the result of the return.

What needs to be explained is: When you call the method of the web service that requires the parameter, you don't have to worry about whether the type of incoming parameter is correct. The web service code will automatically ensure the correctness of the incoming parameter type, in the above example type parameter type For integer, if the malicious user attempts to introduce parameters such as 0 'Malicious SQL Statement to Web Services: Annot Convert 0' Malicious Sql to System.Int32. Parameter Name: Type -> Input String Was Not in a Correct Format. However, if you pass the parameters of the string type, you should remember to replace a single apostrophe (') into two consecutive apostrophe (' '). Use web services

Above, we created a web service, let's take a look at how other websites use this Web service. For the sake of convenience, we call the "consumers" using the customer site of Web services, and provide a Web service website "producer". The most essential thing is what consumers must know what way to call the producer. If you need a parameter, these parameters must be converted to the XML format for incoming, and the consumer sends an HTTP request to the producer, and indicates the methods and parameters to be called, the parameters can be in the form of the SOAP request or a request header with POST. The form is delivered.

After the producer receives the sent request, unpack the input parameters and invoke the appropriate method of the specified class. If the call is completed, return the result to packet, and then send back to the consumer. Consumers receive the response result, unpack, complete the call of the web service.

Obviously, in fact, we don't have to worry about the semantics of the HTTP information sent when using the web service. In order to achieve this, we can use a class called Proxy, and the role of Proxy is acting as a consumer application or web page and production. The intermediate process between the actual web services. For each method of the producer web service, it is also the same method in the Proxy class. Proxy 's duty is to process complex messages for all transfer, this complexity is hidden in the Proxy class, we only You need to simply call the method of this class, you don't have to care about the semantics.

At this point, you may be confused, but this confusion can also be understood, which is a very confused topic. The most basic thing to understand is that the HTTP communication between consumers and producers may be complex when the WEB service is called, and there may be a lot of code. What we are more willing to see is that when you use a web service, you call the web service as soon as you use a local component. In order to achieve this goal, we use the Proxy class, its public interface corresponds to the web service method. If you are confused at this time, please see this presentation document http://aspnet.4guysfromrolla.com/code/consumews.ppt, which will explain how to use Web services.

Create an Proxy class with Visual Studio .NET

The Proxy class that creates a web service in Visual Studio .NET is a light and easy-to-see, in the ASP.NET web project, right-click on the "Reference", select Add Web Reference, then a dialog box will pop up For you to enter a URL address, enter http://aspnet.4guysfromrolla.com/ws/aspfaqs.asmx, then you will see the description of this web service (just as you can see directly in the web browser That's that, finally click the Add Reference button, Visual Studio .NET automatically creates a proxy class for you and compiles. When you add it into your project, the namespace of the Proxy class may be your site address, such as: com.4guysfromrolla.aspnet, of course, you can also change to any other name. Calling Web services from your web page is as convenient to call using local components. Suppose you want to display a list of ASP.NET categories (category ID 22) FAQs, we can pass the GetFaQsInsIncategory method in the web service, and bind the returned DataSet to a DataGrid, the code is like the following The following: '. Other HTML content in the ASPX page ... private sub page_load (sender as object, e as eventargs) Create Proxy examples of class Dim consumeWebService as com._4guysfromrolla.aspnet.ASPFAQs Set consumeWebService = New com._4guysfromrolla.aspnet.ASPFAQs' bound to the results GetFAQsInCategory dgCategoryFAQs dgCategoryFAQs.DataSource = consumeWebService.GetFAQsInCategory (22) dgCategoryFAQs.DataBind () End Sub check The above code, you may not understand the call to the com._4guysfromrolla.aspnet.aspfaqs proxy class is actually the call to the remote web service. When calling the GetFaQsInsInsIry method method of the Proxy class, complex data communication (requests for HTTP / response).

in conclusion

In this article, we tell how to create a web service and use it in the ASP.NET page. Microsoft actually has simplified the process of creating and using a web service in .NET. Creating a web service is simple to just create a .asmx file, then write a little code for the web service method, add a macro, and those code look like the local component code. It is also very simple to use Web services, which is also due to the use of the Proxy class. As mentioned earlier, it is also very simple to create a Proxy class using tools such as Visual Studio .NET.

转载请注明原文地址:https://www.9cbs.com/read-110255.html

New Post(0)