调用百度地图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

上一篇:MySQL安装以及简单命令用法


下一篇:Windows家庭版打开或关闭Hyper-V