C # There are many similarities between WebService calling and JS calling. Let's implement it step by step:
Step 1: Create a WebService
This step is the same as calling WebService in JS. For details, visit the following address:
Address: http://www.cnblogs.com/puresoul/archive/2010/08/19/1803567.html
Step 2: Create a page to implement C # Call Web Services
Add a button on the page. The background code is as follows:
Code
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Web;
5 using System. Web. UI;
6 using System. Web. UI. WebControls;
7
8 using MSXML2;
9
10 public partial class Default4: System. Web. UI. Page
11 {
12 protected void Page_Load (object sender, EventArgs e)
13 {
14
15}
16 protected void button#click (object sender, EventArgs e)
17 {
18 // address of the Web service
19 string URL = "http: // localhost/YBWS/WebService. asmx ";
20
21 // splice data
22 string data;
23 data = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ";
24 data = data + "<soap12: Envelope xmlns: xsi = \" external "xmlns: xsd = \" http://www.w3.org/2001/XMLSchema\ "xmlns: soap12 = \" external "> ";
25 data = data + "<soap12: Body> ";
26 data = data + "<HelloWorld xmlns = \" http://tempuri.org/\ "/> ";
27 data = data + "</soap12: Body> ";
28 data = data + "</soap12: Envelope> ";
29
30 // create an asynchronous object (the XMLHTTP object is under MSXML2)
31 XMLHTTP xmlhttp = new XMLHTTP ();
32 xmlhttp. open ("POST", URL, false, null, null );
33 xmlhttp. setRequestHeader ("Content-Type", "application/soap + xml ");
34 xmlhttp. send (data );
35 Response. Write (System. Text. Encoding. UTF8.GetString (byte []) xmlhttp. responseBody ));
36 Response. End ();
37}
38}
39
When you click a button, the effect is as follows: