Web Service: Learning Web Service series with demo (2) -- compiling a simple Web Service

Source: Internet
Author: User
In a preliminary study on Web Services: Learning Web services using demo series (1) -- writing in preschool, this article describes the development environment required before developing Web Services, one thing I want to add here is not only the necessary conditions described in "Web Service Introduction: Learning Web Service series with demo (1) -- writing in preschool, you must also install IIS to deploy the developed web services.
The following is my development environment Configuration:
Operating System: Windows XP tomato garden edition (IIS installed)
Development tools: Microsoft Visual Studio. net2005
Development language: Visual C #

Here, I will not explain the establishment of the Web Services Project and the modification of the Web Services Name. Now let's assume that we have just created a web services project and opened the. CS file in the app_code folder. You will seeCode(The file name and class name in this Code have been changed. Change the default file name and Class Name of web services to myserviceclass ):

1 Using System;
2 Using System. Web;
3 Using System. Web. Services;
4 Using System. Web. Services. Protocols;
5
6 [WebService (namespace =   " Http://tempuri.org/ " )]
7 [Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
8 Public   Class Myserviceclass: system. Web. Services. WebService
9 {
10 Public Myserviceclass ()
11 {
12
13//If you use the designed components, uncomment the following lines:
14//Initializecomponent ();
15}
16
17 [Webmethod]
18 Public   String Helloworld () {
19Return "Hello World";
20}
21 }

It should be noted that in each external Program Before calling the method name, you must add [webmethod] and define the method as a public method. Here, we will delete the default method Hello World () and enter the following method: 1 Public   Bool Login ( String Username, String Password)
2 {
3 If (Username =   " Micky "   && Password =   " Micky0 " )
4 {
5Return True;
6}
7 Else
8 {
9Return False;
10}
11 }  

we can use an external program to call this simple user authentication method. You can test this method in the development environment, click the run button of Visual Studio. net2005, or press F5 on the keyboard to start the project. For example:
click "login", the page content shown in is displayed:
enter "Micky" in username ", enter "micky0" in password and click "call". The following XML Information is returned on the new page: 1 XML version = "1.0" encoding = "UTF-8" ?>
2 Boolean xmlns =" http://tempuri.org/" > true Boolean >

The content in the <Boolean> section is "true". When we click "call" to send username "Micky" and password "micky0" to Web Services, web Services parses a SOAP message containing the HTTP protocol, and obtains the name of the method to be called and the value of the two parameters passed, find the methods marked with [webmethod] and public from the myserviceclass class. If the same method is found, pass the parameter value to the method. The method returns a Boolean value, so we get the result "true ". Of course, if we enter a non-"Micky" value in username or a non-"micky0" value in password, we will get the result "false" (you can test it yourself)
In this case, a simple Web Service is compiled. Next, we will explain in Visual Studio. in the net2005 c/s program, how to call this function is very simple Web Services, and we will continue to use or rewrite this Web Services in the subsequent demo.

Note:This Web Services example is simple and may be understandable to most people, but it is necessary to explain it if you have taken care of some bloggers who have never touched Web Services. If you have any mistakes or questions, please comment out in the "Comments". You are also welcome to join us.ASP. NET Ajax technology alliance QQ Group.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.