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

发布时间:2020-07-30 13:08:18 作者:逆光翱翔
来源:网络 阅读:2585
        #region Get请求
        /// <summary>  	
        /// HTTP GET方式请求数据.  	
        /// </summary>  	
        /// <param name="url">URL.</param>  	
        /// <returns>返回数据</returns>  	
        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请求
        /// <summary>  	
        /// HTTP POST方式请求数据  	
        /// </summary>  	
        /// <param name="url">URL.</param>  	
        /// <returns></returns>  	
        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方式请求数据
        /// <summary>    
        /// HTTP POST方式请求数据    
        /// </summary>    
        /// <param name="url">URL.</param>    
        /// <param name="param">POST的数据</param>    
        /// <returns></returns>    
        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 非百度经纬度转化为百度经纬度
        /// <summary>
        /// 非百度经纬度转化为百度经纬度
        /// </summary>
        /// <param name="latitude">纬度</param>
        /// <param name="longitude">经度</param>
        /// <returns>百度纬度,经度</returns>
        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;
        }


推荐阅读:
  1. PHP发送POST和GET请求
  2. C# 模拟web   get请求、post请求

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

post get

上一篇:ATS代理缓存工作机制流程图(自画)

下一篇:杨氏矩阵(二维数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》