客户端测试页:
WebService代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Services; 6 using System.Data; 7 using System.Net; 8 using System.Web.Services.Protocols; 9 10 namespace WebSit1 11 { 12 /// <summary> 13 /// Service 的摘要说明 14 /// </summary> 15 [WebService(Namespace = "http://tempuri.org/")] 16 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 17 [System.ComponentModel.ToolboxItem(false)] 18 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 19 // [System.Web.Script.Services.ScriptService] 20 public class Service : System.Web.Services.WebService 21 { 22 23 //[WebMethod] 24 //public string HelloWorld() 25 //{ 26 // return "Hello World"; 27 //} 28 29 [WebMethod(Description="求和的方法",EnableSession=true)] 30 public double addition(double i,double j) 31 { 32 return i + j; 33 } 34 [WebMethod(Description="求差的方法")] 35 public double substruct(double i,double j) 36 { 37 return i - j; 38 } 39 [WebMethod(Description = "求积的方法")] 40 public double multiplication(double i, double j) 41 { 42 return i * j; 43 } 44 [WebMethod(Description = "求商的方法")] 45 public double division(double i, double j) 46 { 47 if (j!=0) 48 { 49 return i / j; 50 } 51 else 52 { 53 return 0; 54 } 55 56 } 57 58 [WebMethod(Description = "得到一个表格的方法")] 59 public DataTable getTable(int j) 60 { 61 DataTable dt = new DataTable("Test"); 62 dt.Columns.Add(new DataColumn("col1", typeof(System.String))); 63 dt.Columns.Add(new DataColumn("col2", typeof(System.String))); 64 65 for (int i = 0; i < j; i++) 66 { 67 DataRow dr = dt.NewRow(); 68 dr[0] = i.ToString(); 69 dr[1] = i.ToString(); 70 dt.Rows.Add(dr); 71 } 72 return dt; 73 74 } 75 [WebMethod(EnableSession=true)] 76 public string TestSession() 77 { 78 string s = "TestSession"; 79 object o = Session[s]; 80 int i = o != null ? (int)o : 0; 81 i++; 82 Session[s]=i; 83 return Session.SessionID + ":" + i; 84 } 85 86 public myHeader header; 87 [WebMethod(Description="测试soap")] 88 [SoapHeader("header",Direction=SoapHeaderDirection.In )] 89 public string TestSoapHeadIn() 90 { 91 return header.name+":"+header.psw; 92 } 93 } 94 95 96 public class myHeader:SoapHeader 97 { 98 public string name; 99 public string psw; 100 } 101 }
客户端代码:
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 using System.Data; 8 using System.Net; 9 using localhost; 10 11 public partial class _Default : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 Button btn = new Button(); 16 btn.Width = 20; 17 btn.Text = "="; 18 btn.Click += btn_Click; 19 E.Controls.Add(btn); 20 getTable.ServerClick += getTable_ServerClick; 21 testSession.ServerClick += testSession_ServerClick; 22 testSoap.ServerClick += testSoap_ServerClick; 23 } 24 25 void testSoap_ServerClick(object sender, EventArgs e) 26 { 27 localhost.Service ws = new localhost.Service(); 28 myHeader head = new myHeader(); 29 head.name = "n1"; 30 head.psw = "p1"; 31 ws.myHeaderValue = head; 32 TextHeader.Text=ws.TestSoapHeadIn(); 33 } 34 35 void testSession_ServerClick(object sender, EventArgs e) 36 { 37 localhost.Service ws = new localhost.Service(); 38 CookieContainer cookies = new CookieContainer(); 39 ws.CookieContainer = cookies; 40 for (int i = 0; i < 10; i++) 41 { 42 sessionI.Text+= ws.TestSession(); 43 sessionI.Text += " "; 44 } 45 } 46 47 void getTable_ServerClick(object sender, EventArgs e) 48 { 49 TestWS.ServiceSoapClient ws = new TestWS.ServiceSoapClient(); 50 DataTable dt = ws.getTable(5); 51 gv.DataSource = dt; 52 gv.DataBind(); 53 } 54 55 void btn_Click(object sender, EventArgs e) 56 { 57 if (Num1.Text!="" && Num2.Text!="") 58 { 59 TestWS.ServiceSoapClient WS = new TestWS.ServiceSoapClient(); 60 int Oper = selectOper.SelectedIndex; 61 switch (Oper) 62 { 63 case 0: 64 Result.Text = WS.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); 65 break; 66 case 1: 67 Result.Text = WS.substruct(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); 68 break; 69 case 2: 70 Result.Text = WS.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); 71 break; 72 case 3: 73 Result.Text = WS.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); 74 break; 75 } 76 77 } 78 79 } 80 81 }