WebService call method on the client

Source: Internet
Author: User

The. NET platform has built-in support for Web Services, including the construction and use of web services. Unlike other development platforms, you can use the. NET platform to develop Web services without other tools or sdks .. Net Framework itself fully supports Web services, including server-side request processors and support for sending and receiving soap messages to clients. This article will show you how to use. Net to create and use web services.

  1. Create a Web Service in. net

To create a web service in. net, you only need to select "file/Add new Project" in your solution. The dialog box shown in 1 is displayed:

Figure 1

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

The following code returns the WebService method of 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 rsString;SqlConnection cn=new SqlConnection(cnstr);SqlDataAdapter cmd=new SqlDataAdapter("select * from employees",cn);DataSet ds=new DataSet();DataTable tbl;cmd.Fill(ds,"employees");tbl=ds.Tables["employees"];rsString="<table border=\"0\" bgcolor=\"blue\" cellpadding=\"1\"   cellspacing=\"1\"><tr bgcolor=\"white\">";for(int i=0;i<=ds.Tables["employees"].Columns.Count-1;i++){rsString+="<td>"+ds.Tables["employees"].Columns[i].ColumnName+"</td>"; }rsString+="</tr>";for(int i=0;i<tbl.Rows.Count;i++){rsString+="<tr bgcolor=\"white\">";for(int j=0;j<=ds.Tables["employees"].Columns.Count-1;j++){rsString+="<td>"+tbl.Rows[i][j]+"</td>";}rsString+="</tr>";}rsString+="</table>";return rsString;}
     

Therefore, it is convenient to create a WebService in vs.net.

  2. Application of WebService on the client

Through the Long-Term Application and Research of vs.net, We have summarized the following four main methods of WebService application on the client:

1. Call the WebService method in the same solution;

2. Call the WebService method in different solutions;

3. Call the WebService method on the Internet;

4. Use WebService. HTC to call the WebService method.

The following describes the above four methods.

1. Call the WebService method in the same solution

First, we need to create a solution named mytest. sln. This scheme consists of a web form named testform. aspx and a WebService named testservice. asmx. The testform. aspx code 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="<table border=\"0\" bgcolor=\"blue\" cellpadding=\"1\"   cellspacing=\"1\"><tr bgcolor=\"white\">";for(int i=0;i<=ds.Tables["employees"].Columns.Count-1;i++){rsString+="<td>"+ds.Tables["employees"].Columns[i].ColumnName+"</td>"; }rsString+="</tr>";for(int i=0;i<tbl.Rows.Count;i++){rsString+="<tr bgcolor=\"white\">";for(int j=0;j<=ds.Tables["employees"].Columns.Count-1;j++){rsString+="<td>"+tbl.Rows[i][j]+"</td>";}rsString+="</tr>";}rsString+="</table>";return rsString;}
     

From the code above, we can see that in the testform, We only provide a testwebservice button. When we click testwebservice, use <span> MSG to display the return values of methods in testwebservice.

In testwebservice. asmx, we create only one method. The code for this method is as follows:

     
      [WebMethod]public string getEmployees(){string cnstr="server=njim01;database=northwind;uid=sa;pwd=6408;";string rsString;SqlConnection cn=new SqlConnection(cnstr);SqlDataAdapter cmd=new SqlDataAdapter("select * from employees",cn);DataSet ds=new DataSet();DataTable tbl;cmd.Fill(ds,"employees");tbl=ds.Tables["employees"];rsString="<table border=\"0\" bgcolor=\"blue\" cellpadding=\"1\"   cellspacing=\"1\"><tr bgcolor=\"white\">";for(int i=0;i<=ds.Tables["employees"].Columns.Count-1;i++){rsString+="<td>"+ds.Tables["employees"].Columns[i].ColumnName+"</td>"; }rsString+="</tr>";for(int i=0;i<tbl.Rows.Count;i++){rsString+="<tr bgcolor=\"white\">";for(int j=0;j<=ds.Tables["employees"].Columns.Count-1;j++){rsString+="<td>"+tbl.Rows[i][j]+"</td>";}rsString+="</tr>";}rsString+="</table>";return rsString;}
     

     
      private void Button4_Click(object sender, System.EventArgs e){testWebService webservice=new testWebService();msg.InnerHtml=webservice.getCustomers();}
     

In fact, in the same solution, the WebService method calls in this solution are defined and called by class.

2. Call the WebService method in different solutions. It is possible that our WebService is not created in the same solution. For example, we have already created it in the previous solution. Now we need to call its method in the new solution.

There are two scenarios:

1. Although WebService is not in the solution to call it, it is on the same physical host;

2. the WebService and solution are not on the same physical host. For example, if we want to call Microsoft's WebService, it is in:

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

Services/dataservice/Vb/dataservice. asmx

Location. How to call it?

1) There are two calling methods on the same physical Host:

① Directly reference the DLL containing the WebService solution. For example

In the WebService. sln solution, a WebService named math. asmx is created. In this WebService, we compile a method:

     
      [WebMethod]public float add(float x,float y){ return x+y;}
     

Next we will call this method in the testform. aspx form:

First, we add a new button in the testform. aspx form named reference.

Secondly, we add WebService. dll in the reference of mytest. sln solution.

Third, the reference_click () encoding of the reference button is as follows:

     
      
Private void reference_click (Object sender, system. eventargs e) {float X, Y; WebService. math math1 = new WebService. math ();/* defines the 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 result is displayed on <span> with the MSG name */}
     

When you click the reference button, the result of the add () method call is displayed in the <span> flag named MSG.

② Web reference in fact, vs.net provides extremely powerful functions, so that we do not need to directly reference a WebService solution DLL. Right-click "Reference" and select "add web reference ...... ", The window shown in 2 is displayed:

Figure 2

Enter the location of the WebService to be called in the address bar, as shown in figure

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

In this case, a "Web reference" folder is added under mytest. sln, which contains a "localhost1" project, such as 3:

Figure 3

In the figure, localhost1 in the Web reference folder references WebService in another solution. The following describes how to call the WebService method in the testform. aspx form using this reference method:

First, add a new button in the testform. aspx form named localhost1.

Encode the localhost1_click () event in the localhost1 button as follows:

     
      
Private void localhost1_click (Object sender, system. eventargs e) {float X, Y; localhost1.math math1 = new localhost1.math ();/* defines the 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 result is displayed on <span> with the MSG name */}
     

When we click the localhost1 button, this code also completes the corresponding method call.

3. Calling the WebService method on the Internet. In fact, the "Web reference" method in 2nd cases is a special case of the current situation. Because, when we change the address in the Web reference address bar to a WebService on a host on the Internet, the WebService method is called on the Internet.

Let's call the WebService sample in Microsoft Asp.net Quick Start. When adding "Web reference", enter the following WebService address in the address bar:

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

Services/dataservice/Vb/dataservice. asmx

3. In the "Web reference" folder, you can see that a new project "com. gotdotnet. CHS ", in testform. in the aspx form, add a button gowebservice, and encode it in the click () event of gowebservice as follows:

     
      
Private void gowebservice_click (Object sender, system. eventargs e) {COM. gotdotnet. CHS. dataservice gows = new COM. gotdotnet. CHS. dataservice ();/* defines the WebService object gows */dataset DS = new dataset (); DS = gows. gettitleauthors ();/* The gettitleauthors () method of the gows object is called here. This method returns a DataSet object */MSG. innerhtml = Ds. tables ["Authors"]. rows. count. tostring ();/* display the number of records in the authors table on <span> MSG */}
     

When you click the gowebservice button, the call to the WebService sample in Microsoft Asp.net Quick Start on the Internet will also be displayed on MSG.

4. Using WebService. HTC to call the WebService method is not described in detail. Please refer to the Microsoft Site.

<B> Conclusion 3 </B>

In short, WebService is very powerful, but it is difficult to find the corresponding introduction on how to call WebService methods on the client. As a result, most developers use the 4th methods, I have summarized the above methods based on my own application experience on vs.net, which is for reference only by vs.net developers!

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.