Analysis of the application method of WebService client

xiaoxiao2021-03-06  68

The .NET platform has built support for Web Service, including the build and use of Web Service. Unlike other development platforms, use the .NET platform, you don't need other tools or SDKs to complete the development of Web Service. The .NET Framework itself fully supports Web Service, including server-side request processors, and support for clients to send and accept SOAP messages. This article will take you with .NET creation and use Web Service.

Create a web service in .NET

To create a web service in .NET, you only need to select "File / Add New Item" in your solution, pop up the dialog box shown in Figure 1:

figure 1

In this box, select "Web Service" and specify the name. VS.NET will create a default WebService framework. You can build the WebService method you need as needed.

The following code is a WebService method that returns all Employee information from the Northwind database Employees of SQL Server 2000.

[WebMethod]

Public string geteMPloyees ()

{

String cnstr = "server = njim01; database = northwind; UID = SA; PWD = 64084888;";

String rstring;

SqlConnection CN = New SqlConnection (CNST);

SqlDataAdapter cmd = new SqldataAdapter ("Select * from Employees", CN);

DataSet DS = New Dataset ();

DataTable TBL;

Cmd.Fill (DS, "Employees");

TBL = DS.TABLES ["Employees"];

rsString = "

Cellspacing = / "1 /">

";

For (int i = 0; i <= ds.tables ["employees"]. columns.count-1; i )

{

Rsstring = "

";

For (int J = 0; j <= ds.tables ["Employees"]. Columns.count-1; J )

{

rsString = "

" DS.Tables ["Employees"]. Column [i] .columnname "";

}

rsString = "";

For (int i = 0; i

{

rsString = "

" TBL.ROWS [I] [J] "";

}

rsString = "";

}

rsString = ""; return rsString;

}

It can be seen that it is convenient to create a webservice in VS.NET.

II WebService Application in Client

Through long-term applications and research on VS.NET, we summarize the WEBSERVICE's approach to client applications mainly following four possible:

1. Call the WebService method in the same solution;

2. Call the WebService method in different solutions;

3. Tune the WebService method on the Internet;

4. Call the WebService method using WebService.htc.

The above four methods are described below.

1. Call the WebService method in the same solution

First, we need to build a solution called MyTest.sln. The solution consists of a Web form called TestForm.aspx and a WebService of TestService.asmx. The code for TestForm.aspx is as follows:

SqlDataAdapter cmd = new SqldataAdapter ("Select * from Employees", CN);

DataSet DS = New Dataset ();

DataTable TBL;

Cmd.Fill (DS, "Employees");

TBL = DS.TABLES ["Employees"];

rsString = "

Cellspacing = / "1 /">

";

For (int i = 0; i <= ds.tables ["employees"]. columns.count-1; i )

{

Rsstring = "

";

For (int J = 0; j <= ds.tables ["Employees"]. Columns.count-1; J )

{

rsString = "

" DS.Tables ["Employees"]. Column [i] .columnname "";

}

rsString = "";

For (int i = 0; i

{

rsString = "

" TBL.ROWS [I] [J] "";

}

rsString = "";

}

rsString = "";

Return rstring;

}

As can be seen from the above code, in the TestForm form, we only provide a TestWebService button. When we click TestWebService, use a named MSG's to display the return value of the method in the TestWebService.

In TestWebService.asmx, we only build a method, the code code is as follows: [WebMethod]

Public string geteMPloyees ()

{

String cnstr = "server = njim01; data = northwind; uid = sa; pwd = 6408;";

String rstring;

SqlConnection CN = New SqlConnection (CNST);

SqlDataAdapter cmd = new SqldataAdapter ("Select * from Employees", CN);

DataSet DS = New Dataset ();

DataTable TBL;

Cmd.Fill (DS, "Employees");

TBL = DS.TABLES ["Employees"];

rsString = "

Cellspacing = / "1 /">

";

For (int i = 0; i <= ds.tables ["employees"]. columns.count-1; i )

{

Rsstring = "

";

For (int J = 0; j <= ds.tables ["Employees"]. Columns.count-1; J )

{

rsString = "

" DS.Tables ["Employees"]. Column [i] .columnname "";

}

rsString = "";

For (int i = 0; i

{

rsString = "

" TBL.ROWS [I] [J] "";

}

rsString = "";

}

rsString = "";

Return rstring;

}

Private void button4_click (Object Sender, System.Eventargs E)

{

TestWebservice WebService = new testwebservice ();

Msg.innerhtml = WebService.getCustomers ();

}

In fact, the call to the WebService method in this program is defined and called by the class.

2. Calling the WebService method in different solutions It is possible that our WebService is not built in the same solution, such as our previous scenario already established. Now we have to call it in a new solution.

It can be divided into two cases:

1, although WebService is not to call its solution, but on the same physical host;

2, WebService and solutions are not on the same physical host, as we want to call Microsoft's WebService sample, it is: http://chs.gotdot.com/quickstart/aspplus/samples/

Services / DataService / VB / DataService.asmx

position. How to call?

1) There are also two ways to call on the same physical host:

1 Direct reference directly to the DLL containing the WebService solution. For example, we are in another name

A WebService named Math.asmx is built in the WebService.sln solution. We wrote a method in this WebService:

[WebMethod]

Public Float Add (Float X, Float Y)

{

Return X Y;

}

Below we have to call this method in the TestForm.aspx form:

First, we add a new button in the TestForm.aspx form, name it as Reference.

Second, we added webservice.dll from the reference of MyTest.sln solution.

Third, the Reference_Click () encoding for the Reference button is as follows:

Private void Reference_Click (Object Sender, System.Eventargs E)

{

Float X, Y;

WebService.math Math1 = New WebService.math ();

/ * Defined here WebService object Math1 * /

X = 11.88F;

Y = 23.19f;

Msg.innerhtml = Math1.Add (x, y) .tostring ();

/ * The add () method of the Math1 object is called here, and the results are displayed in on behalf of MSG * /

}

When we click the Reference button, the result of the add () method call is displayed in the tag named MSG.

2Web Quote In fact, VS.NET provides an incomparable function that makes it unnecessary to reference a WebService solution DLL. We only need to right click on "reference", select "Add Web Reference ...", which pops up the window shown in Figure 2:

figure 2

Enter the location where you want to call the WebService in the address bar, such as

Http: // localhost: 8088 / WebService / Math.asmx

At this point, a "Web Reference" folder is added under Mytest.sln, and there is a "localhost1" item under this folder, as shown in Figure 3 below:

image 3

The LocalHost1 under the web reference folder is a WebService reference to another solution. Let's take a look at how to call this WebService method in this reference method in this reference method:

First, we add a new button in the Testform.aspx form and name LocalHost1.

The localhost1_click () event in the localhost1 button is encoded as follows:

Private void Localhost1_click (Object Sender, System.Eventargs E)

{

Float X, Y;

Localhost1.math Math1 = new localhost1.math ();

/ * Defined here WebService object Math1 * /

X = 16.1f;

y = 17.89f;

MSG.INNNERHTML = Math1.Add (x, y) .tostring (); / * The Add () method of the Math1 object is called here, and the result is displayed in on behalf of MSG * /

}

This piece of code also completed the corresponding method call when we clicked the localhost1 button.

3. Tune the WebService method on the Internet in fact, "Web Reference" mode in the second case is a special case. Because, when we change the address of the web reference address to a WebService method on a host on a host on the Internet, we call the WebService method on the Internet.

Let us call the WebService sample in Microsoft ASP.NET to quickly get started, when we add "Web Reference", enter the following WebService address in the address bar:

Http://chs.gotdotnet.com/quickstart/aspplus/samples/

Services / DataService / VB / DataService.asmx

As shown in Figure 3, you can see a new project "com.gotdotnet.chs" under the "Web Reference" folder, add a button GOWEBSERVICE in the TestForm.aspx form, at the GowebService Click () event The encoding is as follows:

Private Void GowebService_Click (Object Sender, System.Eventargs E)

{

com.gotdotnet.chs.datasempice gows = new com.gotdotnet.chs.dataasemp ();

/ * Defined here WebService object gows * /

DataSet DS = New Dataset ();

DS = GOWS.GETTIAUTHORS ();

/ * Call the getTitLeauthors () method of the GOWS object here, this method returns a DataSet object * /

Msg.innerhtml = ds.tables ["authors"]. rows.count.tostring ();

/ * The number of records of the Authors table is displayed in on the name MSG * /

}

When we click the GOWEBService button, the call to the WebService sample in Microsoft ASP.NET on the Internet is also displayed on the MSG.

4. Use the WebService.htc to call the WebService method This method is not described. Please see the Microsoft site.

Three Connectations

In short, WebService is very powerful, but how to call the WebService method in the client, it is difficult to find the corresponding introduction, so that most developers use the fourth method, I summarize the above methods according to my own application experience, VS.NET developer reference only!

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

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.051, SQL: 9