Analysis of the application method of WebService client

xiaoxiao2021-03-06  63

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. One to create a Web Service in .NET To create a web service in .NET, you only need to select "File / Add Item" in your solution, pop up the dialog box shown in Figure 1: Figure 1 In this box In the "Web Service", select 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. Second WebService's application in the client through the long-term application and research of VS.NET, we summarize the WEBSERVICE to the client application. There are four possibilities: 1, call the WebService method in the same solution; 2, in different The WebService method is called in the solution; 3. Tune the WebService method on the Internet; 4, use the WebService.htc to call the WebService method. 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, the WebService and solutions are not on the same physical host, such as we want to call Microsoft WebService Sample, it is: http://chs.gotdotnet.com/quickstart/aspplus/sample/ services / datasemp / vb / datasempice.asmx location. 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 have established a WebService named Math.asmx in another solution called WebService.sln, and 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 In the address bar, you want to call the WebService location, such as http: // localhost: 8088 / webservice / math.asmx, add a "Web Reference" folder under MyTest.sln, in this folder There is a "localhost1" project, as shown in Figure 3:

Figure 3 of the Localhost1 under the web reference folder is a WebService reference in another solution. Let's see how to call this WebService method in this reference method: First, we add a new button in the TestForm.aspx form, named 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.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 * /

}

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" in the "Web Reference" folder, add one in the TestForm.aspx form. Button GowebService, encoded in the click () event in the GoEbservice 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

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

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