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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#调用百度API方法,POST和GET请求相关的知识,希望对你有一定的参考价值。

        #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;
        }


本文出自 “梦里开花,梦里飞” 博客,请务必保留此出处http://wppeng.blog.51cto.com/9894483/1884452

以上是关于C#调用百度API方法,POST和GET请求的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud Feign调用服务接口时,GET请求变成POST请求报错 _

C#用HttpClient类post get,怎么设置cookies

c# webapi post多个参数

.Net(C#)后台发送Get和Post请求的几种方法总结

http请求POST和GET调用接口以及反射动态调用Webservices类

如何向网站提交一个get 请求 ; 网站的方法接口然后返回一个对象?