标签:
#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; }相关文章
- 多重影分身——C#中多线程的使用三(调用方法和传参)
- JavaScript日历控件开发 C# 读取 appconfig文件配置数据库连接字符串,和配置文件 List
.ForEach 调用异步方法的意外 ef 增加或者更新的习惯思维 asp.net core导入excel 一个二级联动 - C# 动态创建SQL数据库(二) 在.net core web项目中生成二维码 后台Post/Get 请求接口 方式 WebForm 页面ajax 请求后台页面 方法 实现输入框小数多 自动进位展示,编辑时实际值不变 快速掌握Gif动态图实现代码 C#处理和对接HTTP接口请求
- C#方法定义和调用-2
- c#带参数和返回值的函数 开启线程调用的方法
- C#网页自动登录和提交POST信息的多种方法(转)
- 详解php用curl调用接口方法,get和post两种方式
- C#接口在派生类和外部类中的调用方法示例
- php中运用http调用的GET和POST方法示例
- HttpRequest Get和Post调用其他页面的方法