调用百度地图API
Posted 莫使金樽空对月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用百度地图API相关的知识,希望对你有一定的参考价值。
var APP_ID = "xxx"; var API_KEY = "xxxxx"; var SECRET_KEY = "xxxxxxx"; var client = new Baidu.Aip.Nlp.Nlp(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 String authHost = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="+API_KEY+"&client_secret="+SECRET_KEY+"&"; HttpWebRequest req = WebRequest.Create(authHost) as HttpWebRequest; string strResult = ""; HttpWebResponse response = req.GetResponse() as HttpWebResponse; StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); strResult = sr.ReadToEnd(); JObject obj = (JObject)JsonConvert.DeserializeObject(strResult); string token = obj["access_token"].ToString(); string host = "https://aip.baidubce.com/rpc/2.0/nlp/v1/address?charset=UTF-8&access_token=" + token; Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.ContentType = "application/json"; string str = "{\\"text\\": \\"addressxxxxxxxxxxxxxxx\\"}"; byte[] data = Encoding.UTF8.GetBytes(str.ToString()); request.ContentLength = data.Length; using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } HttpWebResponse resp = (HttpWebResponse)request.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { string result = reader.ReadToEnd(); obj = (JObject)JsonConvert.DeserializeObject(result); }
以上是关于调用百度地图API的主要内容,如果未能解决你的问题,请参考以下文章