C#調(diào)用百度API方法,POST和GET請求

        #region Get請求
        /// <summary>  	
        /// HTTP GET方式請求數(shù)據(jù).  	
        /// </summary>  	
        /// <param name="url">URL.</param>  	
        /// <returns>返回?cái)?shù)據(jù)</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方式請求數(shù)據(jù)  	
        /// </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方式請求數(shù)據(jù)
        /// <summary>    
        /// HTTP POST方式請求數(shù)據(jù)    
        /// </summary>    
        /// <param name="url">URL.</param>    
        /// <param name="param">POST的數(shù)據(jù)</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

使用事例,調(diào)用百度API

	//非百度經(jīng)緯度轉(zhuǎn)化為百度經(jīng)緯度
        public static string Baidull = "http://api.map.baidu.com/geoconv/v1/?coords={0}&from=1&to=5&ak={1}";

	BaidumapAK;//百度開發(fā)者AK;

       #region 非百度經(jīng)緯度轉(zhuǎn)化為百度經(jīng)緯度
        /// <summary>
        /// 非百度經(jīng)緯度轉(zhuǎn)化為百度經(jīng)緯度
        /// </summary>
        /// <param name="latitude">緯度</param>
        /// <param name="longitude">經(jīng)度</param>
        /// <returns>百度緯度,經(jīng)度</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;
        }

新聞名稱:C#調(diào)用百度API方法,POST和GET請求
轉(zhuǎn)載注明:http://m.kartarina.com/article30/geseso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站云服務(wù)器搜索引擎優(yōu)化微信公眾號(hào)網(wǎng)站策劃軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司
主站蜘蛛池模板: 久久亚洲精品无码gv| 国产精品无码不卡一区二区三区| 内射精品无码中文字幕| 国产成人无码AV一区二区在线观看 | 亚洲AV综合色区无码二区爱AV| 国产亚洲精久久久久久无码AV| 久久久久亚洲av成人无码电影 | 亚洲av成本人无码网站| 无码Aⅴ在线观看| 极品粉嫩嫩模大尺度无码视频| 亚洲国产一二三精品无码| 亚洲国产av高清无码| 国产午夜无码视频免费网站| 国产成人精品无码片区在线观看| 人妻精品久久无码区洗澡| 亚洲日韩乱码中文无码蜜桃臀网站| 亚洲AV无码一区二区三区网址| 久久av高潮av无码av喷吹| 亚洲AV中文无码字幕色三 | 亚洲国产精品无码久久久不卡| 亚洲国产成AV人天堂无码| 国产成人无码网站| 无码人妻AⅤ一区二区三区| 黑人无码精品又粗又大又长| 日韩精品少妇无码受不了| 久久亚洲日韩看片无码| 一级片无码中文字幕乱伦| 日韩精品中文字幕无码专区 | 日韩精品少妇无码受不了| 午夜无码一区二区三区在线观看 | 亚洲精品9999久久久久无码| 亚洲精品无码永久在线观看你懂的| 蜜芽亚洲av无码一区二区三区| 亚洲AV人无码激艳猛片| 亚洲va中文字幕无码久久| 免费看国产成年无码AV片| 99精品人妻无码专区在线视频区 | 亚洲av永久无码精品秋霞电影秋| 亚洲AV人无码综合在线观看| 中文字幕av无码一二三区电影| 国产在线无码视频一区二区三区 |