Tutorial on WebService services and AJAX usage

Source: Internet
Author: User
Tags config http post soap return xmlns wsdl
ajax|web| Tutorial

digression :

WebService technology has several years of history, about the theoretical knowledge of the foundation, omitted here, does not say. The Ajax technology that has been fired recently has also been abused very seriously, as far as details are concerned, I am not talking about the point.

The introduction of AJAX technology is relatively low, JavaScript and XML a little use, the individual thought that there is no content, can not fundamentally solve the enterprise's needs or problems, security, transmission of low-level and other aspects have fatal problems.

Why put WebService and Ajax together, for the simple reason that the two have common ground is that they often--------XML according to the same data protocol.

This article all source code :userapp.rar

This article neither explains the weakness of Ajax, nor does it webservice powerful, but illustrates how to use WebService technology and AJAX technology in an example.

I show how by example "User Login".
Create a virtual directory Userapp locally.

First create a WebService:

Namespace cjjer{
Using System;
Using System.Web.Services;
[WebService (Name= "Webscjjer", description= "a Web service that validates user login", namespace= "http://www.cjjer.com/webs/")]
public class userservice{
[WebMethod (Description = "Enter username and secret, return permission int value, 0 indicates failure", messagename= "Login")]
public int Login (string username,string userpassword) {
Return User.check (Username,userpassword);
}


};
}

We do not look at the details for the moment, note that this class has a header webservice, marking this is a Web service.

The CSC compiles, puts it into the bin directory, and then login.asmx the file:

<% @WebService class= "Cjjer.userservice"%>

I will not say the details of the verification first, let's assume that User.check () returns the user level, and 0 indicates that the validation failed.

Now enter the address in the URL, Access: as shown in

Click Login:

Enter the correct username and password, and then click Call:

If it is not the correct username and password, the int node value is 0.

Our local call is in the soap operation.

There are many other ways:

SOAP(lowest level)

Soap here does not say, in the future will say in detail, I am here to look at the Get and post way:

HTTP get

Face is an example of an HTTP GET request and response. The displayed placeholder needs to be replaced by the actual value.

Get/userapp/login.asmx/login? Username=string&userpassword=string http/1.1
Host:s

Return

http/1.1 OK
Content-type:text/xml; Charset=utf-8
Content-length:length

<?xml version= "1.0" encoding= "Utf-8"?>
<int xmlns= "http://www.cjjer.com/webs/" >int</int>

HTTP POST

The following is an example of an HTTP POST request and response. The displayed placeholder needs to be replaced by the actual value.

Post/userapp/login.asmx/login http/1.1
Host:s
content-type:application/x-www-form-urlencoded
Content-length:length

Username=string&userpassword=string

Return

http/1.1 OK
Content-type:text/xml; Charset=utf-8
Content-length:length

<?xml version= "1.0" encoding= "Utf-8"?>
<int xmlns= "http://www.cjjer.com/webs/" >int</int>

We can enter the address of the URL, or post action, to access and submit the results.

Here you can choose a lot of operations, the use of screen cleaning technology, you can also use the XML processing framework to submit the results of the information, but also with other techniques, do not say, anyway, it seems to need dozens of lines or more than I do below some processing.

I'm talking about a Web service that uses the SDK's tool WSDL tool to process proxies.

Wsdl/l:cs/out:webscjjer.cs/namespace:webs/urlkey:websuser/nologo http://s:81/userapp/login.asmx?wsdl

Get WebsCjjer.cs source files.

Note :/urlkey:websuser indicates that the URL can be obtained from the websuser of the Web.config file, and this is done to avoid hard coding and see WebsCjjer.cs

.......
Public Webscjjer () {
String urlsetting = system.configuration.configurationsettings.appsettings["Websuser"];
if ((urlsetting!= null)) {
This. URL = urlsetting;
}
else {
This. URL = "Http://s:81/userapp/login.asmx";
}
}
.......

In order to be accessible to other people, you need to add the following code to the web.config:

<system.web>
<webServices>
<protocols>
<add name= "HttpPost"/>
<add name= "HttpGet"/>
</protocols>
</webServices>

Is the agent's license.

Compile this class to the bin, when used as follows:

void Onlogin (Object S,eventargs e) {
Webs.webscjjer Ws_app = new Webs.webscjjer ();
ReturnInt. Text = Ws_app.login (Myusername.text,mypassword.text). ToString ();
}

In this way, the results can be returned directly, without our own regular or XML processing, no need to know the details of the validation .



Related Article

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.