How to create and use Web services

zhaozj2021-02-11  173

How to create and use Web services

Introduction. One of the most powerful aspects of Net is that it can be used 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/). Creating 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 (FAQs). 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 finally provides the following functions 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 A common class is created, 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.

Here is the code to achieve: <% @ 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 a DataSet Return myDataSet End Function 'Creating implementation of three tasks public function getcategories () AS Dataset Return getDataSet (SQL Query)' Here the parameter SQL is used to get all FAQ classificions End function public ()> public ()> public ()> public ()> public ()> Function getfaqsincategory (Catid As Integer) AS Dataset Return getDataSet 'Here the parameter SQL is used to get all faq end function public function getFAQ (FAQID AS Integer) AS Dataset Return GetDataSet (SQL Query)' here's parameter SQL Used to get a FAQ (FAQID) End Function End Class, as mentioned earlier, three methods accessed through web services include preamble , there is a @ @ASMX file WebService indicates the language and class name used. 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 (' '). With the web service, 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.

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

New Post(0)