网站如何调用api接口
直接GET一个URL过去比如说<form action="http://api.36wu.com/Weather/GetWeather?district=%E5%8C%97%E4%BA%AC" method="get"><input type="submit" value="提交" /></form>实际上,你应该是用JS或者JQuery库来获取, API接口信息你如果想使用这种接口那么你可以在后台使用httpwebrequest或者webclient模拟post|get请求 然后得到数据.2、WebService接口信息webservice就更好办啦 你添加这webservice 然后调用就行了.
操作方法
- 01
T是他返回数据的对象 /// <summary> /// Post获取信息 /// </summary> /// <typeparam name="T">实体对象</typeparam> /// <param name="url">post url</param> /// <param name="data">post data</param> /// <returns></returns> public static T PostOfUrl<T>(string url, string data) { System.Net.HttpWebRequest httpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url); httpWebRequest.Method = "POST"; httpWebRequest.Headers.Add("Accept-Charset", "utf-8"); byte[] postBytes = Encoding.UTF8.GetBytes(data); //httpWebRequest.ContentType = "text/xml"; httpWebRequest.ContentType = "application/json; charset=utf-8";// httpWebRequest.ContentLength = Encoding.UTF8.GetByteCount(data);//strJson为json字符串 Stream stream = httpWebRequest.GetRequestStream(); stream.Write(postBytes, 0, postBytes.Length); stream.Close();//发送完毕,接受返回值 var response = httpWebRequest.GetResponse(); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); String responseString = streamRead.ReadToEnd(); JavaScriptSerializer js = new JavaScriptSerializer(); var list = js.Deserialize<T>(responseString); return list; } public static T GetOfUrl<T>(string url) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8); string respHtml = sr.ReadToEnd(); JavaScriptSerializer js = new JavaScriptSerializer(); var list = js.Deserialize<T>(respHtml); return list; }