#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