C # call Baidu API method, post and GET request

Source: Internet
Author: User

         #region  get Request          /// <summary>          /// HTTP  Get method Request Data .          /// </summary>           /// <param name= "url" >URL.</param>           /// <returns> Return Data </returns>           private static string httpget (String url)         {             HttpWebRequest request =  (HttpWebRequest) httpwebrequest.create (URL);             request. method =  "GET";       &nBsp;     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 Request          /// <summary>          /// HTTP  Post mode request Data           /// </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 =  "*/*";      &nbSp;      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)         & nbsp;   {                 throw;            }             finally             {                 request = null;                 response = null;             }            return responseStr;         }         #endregion           #region  http post Way to request data          /// <summary>            /// http  Post mode request Data             /// </summary>             /// <param name= "url" > Url.</param>            /// <param  name= "param" >post data </param>             /// <returns></returns>             public static string httppost (String url, string param)          {             httpwebrequest request =  (HttpWebRequest) httpwebrequest.create (URL);         &Nbsp;   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使用事例, call Baidu api//non-Baidu latitude and longitude conversion to Baidu latitude and longitude of         public static  string baidull =  "Http://api.map.baidu.com/geoconv/v1/?coords={0}&from=1&to=5&ak={1}"; baidumapak;//Baidu Developer ak;        #region   non-Baidu latitude and longitude conversion to Baidu latitude and longitude          /// <summary>        ///   Non-Baidu latitude and longitude conversion to Baidu latitude and longitude         /// </summary>         /// <param name= "Latitude" > Latitude </param>         /// <param name= "Longitude" > Longitude </param>         /// <returns> latitude of Baidu, longitude </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;         }


This article from "Dream Blossom, Dream fly" blog, please be sure to keep this source http://wppeng.blog.51cto.com/9894483/1884452

C # call Baidu API method, post and GET request

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.