Web|web Services | create
Brief introduction
. One of the most powerful aspects of net is that it can be used to create Web services. A Web service is an external interface that a Web site provides for other Web sites to invoke. For example, a financial company can provide a detailed stock quote to its trading partner through a Web service that can be read and displayed through a Web page, or read from a client's desktop application.
This article describes two aspects of Web services: How to create a Web service, and how to use a Web service. As an example, we explain how to create a Web service with a Web service from the FAQs (FAQ) of aspfaqs.com (http://www.aspfaqs.com/aspfaqs/).
Creating a Web Service
Before you create a Web service, you must first ask yourself: "What services do I offer to my users?" ”。 The goal of this article is to create a Web service that lets other users display a list of frequently asked questions (FAQS) from aspfaqs.com on their own web site. The ideal function is to limit other sites to view FAQs categories and FAQs, and if you want to see an answer to a question, let the user visit the site that provides the service http://www.aspfaqs.com. The Web services in this article will ultimately provide the following features to other Web sites:
1, browse through the list of all FAQ categories
2, browse all the FAQs in a particular category.
3, browse a question for a FAQ, but don't include an answer.
Creating a Web service is simple, creating an. asmx file first (you can use Visual Studio.) NET or any text editor you like, it is recommended to use the Web Matrix, it has a template for creating Web services, and Web services are created as a common class, and there is a macro in front of the method that indicates that this method is accessed through a Web service.
For aspfaqs.com Web services, first create three methods that are accessed through Web services, GetCategories, Getfaqsincategory, and Getfaq, respectively, to implement the tasks described above 1,2,3. and create a private method GetDataSet, which is assembled into a dataset by the SQL query passed over. Here is the implementation code:
&lt;%@ WebService language= "VB" class= "Aspfaqs"%&gt;imports System.Web.ServicesImports system.dataimports System.Data.SqlClientImports system.configurationpublic Class Aspfaqs ' Create private functionality method GetDataSet Private Function GetDataSet (strSQL as String) as DataSet ' 1. Create a SqlConnection Connection object Dim MyConnection as New SqlConnection (ConnectionString) ' 2. Create command object, incoming SQL parameter Dim mycommand as New SqlCommand (strSQL, MyConnection) Myconnection.open () ' 3. Create DataAdapter Object Dim MyDataAdapter as New SqlDataAdapter () Mydataadapter.selectcommand = MyCommand ' 4. Generate dataset and close connection Dim myDataSet as New DataSet () Mydataadapter.fill (myDataSet) myconnection.close () ' returns to DataSet return myDataSet End Function ' Creates a method to implement three tasks <webmethod () > Public Function getcategories () as DataSet return GetDataSet (SQ L Query) ' Here's the parameter SQL to get all FAQ category End Function <webmethod () > Public Function getfaqsincategory (CatID as Integer) as Data Set return GetDataSet (SQL Query) ' This parameter SQL is used to get all the FAQ end Function <webmethod () for a category (CatID)> Public Function getfaq (faqid as Integer) as DataSet return GetDataSet (SQL Query) ' This parameter SQL is used to get the information end of a FAQ (faqid) Func Tionend Class
As mentioned earlier, three methods accessed through a Web service have a leader character , and a @webservice in the first line of the. asmx file indicates the language and class name used. This web service is named Aspfaqs, and the class has the same name. Once you have created the. asmx file, you save it to a directory that is accessible through the Web, and then you can access it through a Web browser. For example: If the Web Service file name is Aspfaqs.asmx and saved to the/ws directory, then anyone can pass http://aspnet.4guysfromrolla.com/WS/ Aspfaqs.asmx access, you can see all the public methods used by the Web service, and you can also provide input parameters to see the results of the return.
What needs to be stated is: When you make a call to a method of a Web service that requires parameters, you do not have to worry about whether the type of the incoming argument is correct, and the Web Service code automatically guarantees the correctness of the incoming parameter type, in which case the parameter type is integral, and if a malicious user attempts to pass the image 0 Malicious SQL statement This parameter returns the wrong message: Annot convert 0 ' malicious SQL to System.Int32. Parameter name:type--> Input string is not in a correct format. However, if you pass in a string-type argument, you should remember to replace the single apostrophe (') with two consecutive apostrophes (').
Using Web Services
Above, we created a Web service, and here's a look at how other sites use this Web service. For convenience, we call the customer website that uses Web services the "consumer", which provides the Web Services Web site to the "producer". The most essential thing is that consumers must know what to call the producer. If parameters are required, these parameters must be converted into XML format for incoming, the consumer sends an HTTP request to the producer and indicates the method and parameter to invoke, which can be passed in the form of a QueryString SOAP request or as a Post's request header.
The producer receives the request sent over, unpack the input parameters, and invoke the appropriate method for the specified class. If the call completes, the result is returned, packaged, and sent back to the consumer. The consumer receives the response result, carries on the unpack, completes the Web service the invocation.
Obviously, when we use Web services without worrying about the semantics of HTTP messages sent, we can use a class called Proxy, which acts as an intermediate process between a consumer application or a Web page and a producer's actual Web service. For every method of a producer Web service, there is also a similar method in the proxy class, where the responsibility of the proxy is to handle all the transmitted complex messages, which are hidden in the proxy class, and we simply need to invoke the method of the class without concern for the semantics of things.
You may be confused at this point, but this confusion is understandable, which is a very confusing topic in itself. The basic thing to understand is that HTTP communication between the consumer and the producer may be complex when invoking a Web service, and may require a lot of code to be written. What we'd like to see is that pages that use Web services invoke Web services as easily as using a local component, and in order to do that, we use the proxy class, which corresponds to the method of the Web service's public interface. If you're still confused at this point, check out this demo document HTTP://ASPNET.4GUYSFROMROLLA.COM/CODE/CONSUMEWS.PPT, which will explain how to use Web services.
Take advantage of visual Studio. NET Create proxy class
In Visual Studio. NET to create a Web service used by the proxy class is easy to do, in the ASP.net Web project, on the "Reference" right-click, select "Add Web Reference", then a dialog box pops up, you enter a URL address, please enter http:// Aspnet.4guysfromrolla.com/ws/aspfaqs.asmx, then you'll see the description of the Web service (as you can see directly in your Web browser), and finally click the Add Reference button, Visual Studio. NET will automatically create a proxy class for you and compile it. When you add it to your project, the proxy namespace may be your website address, such as: Com.4guysfromrolla.aspnet, and of course you can arbitrarily change any other name. Calling a Web service from a proxy class from your Web page is as handy as using a local component to invoke a call. Suppose you want to display a list of ASP.net categories (category ID) FAQs, we can pass 22 to the parameters by invoking the Getfaqsincategory method of the Web service, and bind the returned dataset to a DataGrid. The code might be as follows:
<xmp>other HTML content in the. aspx page ... Private Sub Page_Load (sender as Object, e as EventArgs) ' Create an instance of a proxy class Dim Consumewebservice as Com._4guysfromrolla.aspnet.a Spfaqs Set consumewebservice = New Com._4guysfromrolla.aspnet.aspfaqs ' binds getfaqsincategory results to Dgcategoryfaqs Dgcategoryfaqs.datasource = Consumewebservice.getfaqsincategory dgcategoryfaqs.databind () End Sub</xmp>
Check the code above and you may not understand that the call to the Com._4guysfromrolla.aspnet.aspfaqs proxy class is actually a call to a remote Web service, when the Getfaqsincategory method of the proxy class is invoked, A complex data communication (HTTP request/response) is performed.
Conclusion
In this article, we talked about how to create a Web service and how to use it in a asp.net page. Microsoft is actually in. NET has simplified the process of creating and using Web services. Creating a Web service is simple enough to simply create an. asmx file and then write a little code for the Web service's method and add macros that look like the local component code. Using Web Services is also simple, thanks to the use of proxy classes. As mentioned earlier, take advantage of such things as visual Studio. NET as a tool to create a proxy class is also very simple.