WEBAPI POST

  • 866
  • 0

摘要:WEBAPI POST

第一次接觸API 不知道在衝啥,直覺想到就是一個難字~既然碰到就要實現API功能嚕。

當然最難的地方就是用MVC呈現 蝦會 看了ASP.NET MVC4網站開發美學

再加上同事的引導 所作出來的WEBAPI 
 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入電子郵件");

            string Email = Console.ReadLine();

            Customer Customer = new Customer { Login = "123", Password = "1234", EmailAddress = Email };

            Uri CustomerID;

            HttpClient client = new HttpClient();
        /*第一放入你的網址列*/
            client.BaseAddress = new Uri("http://www.yahoo.com/");

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        /*這行是放入你的網址參數PostAsJsonAsync  把資料給對方*/
            var response = client.PostAsJsonAsync("WebAPI/Customer.aspx?ManagerLoginName=" + Customer.ManagerLoginName + "&ManagerPassword=" + Customer.ManagerPassword + "&Option=1&EmailAddress=" + Customer.EmailAddress + "", Customer).Result;
            /*檢查是否通過*/
            if (response.IsSuccessStatusCode)
            {
             /*他就會回傳你要的參數*/
                var value = response.Content.ReadAsStringAsync().Result;

                Console.WriteLine(value);



            }

            Console.ReadLine();

        }
        public class Customer
        {
            public string EmailAddress;
            public string ManagerLoginName;
            public string ManagerPassword;
        }




    }
}