WEB API 调用示例

WEB端调用

    var user = {
                    "Id":3,
                    "Name":"Hammer"
                };
 
                $.ajax({
                    url: 'http://localhost:5543/api/Product/PostUser',
                    type: 'POST',
                    data: JSON.stringify(user),
                    headers: {"Accept":"application/json" , "Content-Type":"application/json"},
                    success: function(data) {
                        alert('User added Successfully');
                    },
                    error: function() {
                        alert('User not Added');
                    }
                });

WINFORM端调用

 

public class SimuController : ApiController
    {     
        //EF 5
        BIM_GENERALDICTONARY_DBEntities entities=new BIM_GENERALDICTONARY_DBEntities(); 
        
        // GET api/Simu
        public IEnumerable<T_BIM_PropityClass> Get()
        {
            return entities.T_BIM_PropityClass;
        }
    }
static readonly Uri _baseAddress = new Uri("http://localhost:5373/");
        static readonly Uri _address = new Uri(_baseAddress, "/api/simu");
        private void button1_Click(object sender, EventArgs e)
        {

            WebClient webClient = new WebClient();
            webClient.Headers["Accept"] = "application/json";
            webClient.Encoding = Encoding.UTF8;webClient.DownloadStringCompleted += (send, es) =>
                {
                 if (es.Result != null)
                 {
                     var test = JsonConvert.DeserializeObject<T_BIM_PropityClass[]>(es.Result);
                     if (test.Any())
                     {
                         gridControl1.DataSource = test;}
                 }
                 else
                 {
                     MessageBox.Show(es.Error.Message);
                 }
                };
            webClient.DownloadStringAsync(_address);

        }

 

其它参考:

http://www.cnblogs.com/lori/p/4045413.html

posted on 2015-09-09 11:35  老有所依  阅读(222)  评论(0)    收藏  举报

导航