一、API接口
含義:將需要別人訪問的內容寫在在Controller的action中,讓外部人員能通過http請求訪問action中的內容,前提是api需要發布到外網!
在后台通過http請求訪問別人的api接口獲取信息——》寫成一個類。
需要將本地的信息做好邏輯返回給別人調用時——》寫成一個api接口
獲取和發送一定要分清楚
二、API接口——》后台通過http接口獲取數據
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); request.Method = "get"; request.ContentType = "application/json"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) { encoding = "UTF-8"; //默認編碼 } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); retString = reader.ReadToEnd();//返回數據