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