API接口和WebService服務


一、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();//返回數據

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM