Web Service 使用

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   

SOAP、WSDL、UDDI
SOAP(Simple Object Access Protocal),是在分散或在分布式環境中交換資訊的簡單協議。
WSDL 對WebService 的解釋說明文檔
UDDI (通用發現、說明和整合)是Web服務的黃頁
趣味理解:
Web Service 好比是一個服務供應商;
SOAP 就像是兩個公司之間簽的合約,約束雙方按照一定的規範和標準做事;
WSDL就像說明書,告訴別人你有什麼,能給別人提供什麼服務;
UDDI好比你的公司需要在黃頁或工商註冊,方便別人查詢

TestWebService1.asmx檔案

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;namespace WebService{    /// <summary>    /// TestWebService1 的摘要說明    /// </summary>    [WebService(Namespace = "http://www.anniya.com/api")]//    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    [System.ComponentModel.ToolboxItem(false)]    // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消注釋以下行。     [System.Web.Script.Services.ScriptService]    public class TestWebService1 : System.Web.Services.WebService    {        [WebMethod]        [SoapHeader("myHeader")]        public string HelloWorld(string name)        {            if (!myHeader.IsValid())            {                return "對不起,您沒有許可權訪問";            }            return name + ",Hello World";        }        public MySoapHeader myHeader = new MySoapHeader();        [WebMethod]        public List<Student> GetStudent()        {                       return new List<Student>()            {                new Student() {Id = 1, Name = "張三1"},                new Student() {Id = 2, Name = "張三2"},                new Student() {Id = 3, Name = "張三3"}            };        }       [WebMethod(Description="根據學生列表進行處理")]        public List<Student> GetStudentResultByStudentList(List<Student> student)        {            foreach (var stu in student)            {                stu.Name += ",已經處理過了";            }            return student;        }            }    public class Student    {        public int Id { get; set; }        public string Name { get; set; }    }    public class MySoapHeader : SoapHeader    {        public string UserName { get; set; }        public string   Password { get; set; }        public bool IsValid()        {            if (UserName == "admin" && Password == "123")            {                return true;            }            else            {                return false;            }        }    }    }

調用代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using  WebApplicationTestWebService.ServiceReference1;namespace WebApplicationTestWebService{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            using (TestWebService1SoapClient client = new TestWebService1SoapClient())            {                MySoapHeader myHeader = new MySoapHeader();                myHeader.UserName = "admin";                myHeader.Password = "123";//使用者名稱和密碼要和調用的Web Service 一致                Response.Write(client.HelloWorld(myHeader, "yxl") + "</br>");                Response.Write("<hr/>");                Student[] students = client.GetStudent();                foreach (var student in students)                {                    Response.Write(student.Name+"</br>");                }                Response.Write("<hr/>");                foreach (var student in client.GetStudentResultByStudentList(students))                {                    Response.Write(student.Name + "</br>");                }            }                  }    }}

如果使用ajax調用並且跨域的話,可以在本地應用程式中調用webservice,然後在用js調用本地應用程式,這樣就不會跨域了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.