快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

C#调用百度API方法,POST和GET请求

        #region Get请求
        ///   	
        /// HTTP GET方式请求数据.  	
        ///   	
        /// URL.  	
        /// 返回数据  	
        private static string HttpGet(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;

            WebResponse response = null;
            string responseStr = null;

            try
            {
                response = request.GetResponse();

                if (response != null)
                {
                    StreamReader reader =
    new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                response = null;
            }

            return responseStr;
        }
        #endregion


        #region POST请求
        ///   	
        /// HTTP POST方式请求数据  	
        ///   	
        /// URL.  	
        ///   	
        private static string HttpPost(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;

            WebResponse response = null;
            string responseStr = null;

            try
            {
                response = request.GetResponse();
                if (response != null)
                {
                    StreamReader reader = new
    StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    //File.WriteAllText(Server.MapPath("~/") + @"\test.txt", responseStr);	
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                response = null;
            }

            return responseStr;
        }

        #endregion


        #region HTTP POST方式请求数据
        ///     
        /// HTTP POST方式请求数据    
        ///     
        /// URL.    
        /// POST的数据    
        ///     
        public static string HttpPost(string url, string param)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;

            StreamWriter requestStream = null;
            WebResponse response = null;
            string responseStr = null;

            try
            {
                requestStream = new StreamWriter(request.GetRequestStream());
                requestStream.Write(param);
                requestStream.Close();

                response = request.GetResponse();
                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("~//weixin/Weixin/") + @"\test.txt", responseStr);
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                requestStream = null;
                response = null;
            }

            return responseStr;
        }

        #endregion

使用事例,调用百度API

	//非百度经纬度转化为百度经纬度
        public static string Baidull = "http://api.map.baidu.com/geoconv/v1/?coords={0}&from=1&to=5&ak={1}";

	BaidumapAK;//百度开发者AK;

       #region 非百度经纬度转化为百度经纬度
        /// 
        /// 非百度经纬度转化为百度经纬度
        /// 
        /// 纬度
        /// 经度
        /// 百度纬度,经度
        public static string GetBaidull(string latitude, string longitude)
        {

            string text = "";
            string location = longitude + "," + latitude;
            string url = string.Format(Baidull, location, BaidumapAK);
            string response = HttpGet(url);

            JObject jo = new JObject();
            jo = (JObject)JsonConvert.DeserializeObject(response);
            
            latitude = jo["result"][0]["y"].ToString();
            longitude = jo["result"][0]["x"].ToString();
            text = latitude + "," + longitude;
            
            return text;
        }

网站题目:C#调用百度API方法,POST和GET请求
文章来源:http://6mz.cn/article/geseso.html

其他资讯