Asp. NET and SOAP protocol usage records

Source: Internet
Author: User
Tags soap

Recently took over a company's management system development tasks, because the company needs to have Android,ios client, but also require a PC-side Web Client .... The requirements for service requests naturally fall on the unified interface access ....

Use ASP. NET Web Service (ASMX) to provide a unified access interface to the outside ... All clients exchange data in the form of Post/get data and Web services, and do not care about the processing of the data, nor directly accessing the database, and in the case of Web services, return service response information in JSON data format .... The following records for the learning/development process records, insight superficial, only for their own records, but also to the beginning of a friend to start a guide, shortcomings, hope can forgive, theoretical knowledge of self-search:

You can start by creating a new project (Visual C#-->asp.net Web application). Or create a new Web site (visual c#-->asp.net empty site): [Take a new project as an example: project name Soaptry]

Add a Web service (ASMX) under the project named WebServiceTest

Open the Webservicetest.asmx file: Delete the HelloWorld method (not delete), create a new method to reverse the string reversestring (String message) and a way to verify the login login ( String i_tel,string i_psd):

The source code is as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.Remoting.Messaging;
Using System.Web;
Using System.Web.Services;
Using System.Web.Services.Protocols;

Namespace Soaptry
{
<summary>
Summary description of WebServiceTest
</summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[System.ComponentModel.ToolboxItem (False)]
To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebServiceTest:System.Web.Services.WebService
{
[WebMethod]
public string reversestring (String message)//new This method
{
char[] arr = message. ToCharArray ();
Array.reverse (arr);
message = new string (arr);
return message;
}
[WebMethod]
public string Login (string i_tel,string i_psd)
{
String Tel = I_tel. Trim (). ToString ();
string psd = i_psd. Trim (). ToString ();
if (tel. Equals ("admin") & PSD. Equals ("ASD"))
{
return "Success";

}
Else
{
return "False";
}
}
}

}

Next, let's take a look at the preview of the Web service:

------------------------------------------------------------------------See in the browser:------------------------------- --------------------------------------------

----------------------------------------------------------------------------the picture divider line---------------------------------------- -------------------------------------------

The two methods we wrote here are the two interfaces: we click Login as an example (login is set to determine when ADMIN/ASD input returns success):

----------------------------------------------------------------------------the picture divider line---------------------------------------- -------------------------------------------

----------------------------------------------------------------------------the picture divider line---------------------------------------- -------------------------------------------

After entering the data as shown above, click Invoke:

----------------------------------------------------------------------------the picture divider line---------------------------------------- -------------------------------------------

----------------------------------------------------------------------------the picture divider line---------------------------------------- -------------------------------------------

This completes the data exchange with the Web service ....

The next step is how to use these interfaces with the client:

-------------------------------------------------------How the service is accessed on the client (that is, the. aspx page):---------------------------- -------------------------------

Add New Item-->web form, create a new test.aspx page ... Add a label to Test.aspx:

In the Test.aspx.cs file, add the following code:

View Test.aspx in the browser:

This is just the way the service is accessed by the machine .... We want to access the interface in the Internet environment, for example, we want to access the Weather interface:

First look at the weather forecast interface information: Visit URL: http://ws.webxml.com.cn/WebServices/WeatherWS.asmx

To use someone else's interface, we'll start by referencing the service in our own project:

Then:

Select Advanced Options .... Other options do not move, then select "Add Web Reference":

Enter:

Let's see what's in the Explorer:

If you are creating a Web site at the beginning, after you add a Web service reference, the layout of this folder should look like this:

Of course, in this way, it looks more clear ..... The class we are going to use is the weatherws of the dash .... The circled part is just a namespace ....

In Test.aspx.cs. We change the code to:

Preview:

The interface method under Weatherws is many ..... Not only this one listed the supported provinces of China this one,... Here is just a demo ... Above... So

Asp. NET and SOAP protocol usage records

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.