赞
踩
- string url = "https://restapi.amap.com/v3/distance?origins=abc";
- string res = HttpGet(url);
-
- string url1="http://zxcvbnm/Shop/AddOrder";
- string json = JsonConvert.SerializeObject(new { type = "1", order_id = "2" });
- string result = HttpPost(url1, json, "application/json");
-
-
- /// <summary>
- /// Get请求
- /// </summary>
- /// <param name="url">请求地址</param>
- /// <param name="Timeout">超时时间默认两分钟</param>
- /// <returns></returns>
- public static string HttpGet(string url, int Timeout=120000)
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = "GET";
- request.ContentType = "text/html;charset=UTF-8";
- request.UserAgent = null;
- request.Timeout = Timeout;
-
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream myResponseStream = response.GetResponseStream();
- StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
- string retString = myStreamReader.ReadToEnd();
- myStreamReader.Close();
- myResponseStream.Close();
-
- return retString;
- }
- /// <summary>
- /// Post请求
- /// </summary>
- /// <param name="url">请求地址</param>
- /// <param name="pm_str">请求参数</param>
- /// <param name="pm_ContentType">参数类型</param>
- /// <param name="headstr">请求头</param>
- /// <returns></returns>
- public static string HttpPost(string url, string pm_str, string pm_ContentType = "appication/json", string headstr = null)
- {
- StreamReader reader = null;
- Stream requestStream = null;
- HttpWebResponse Response = null;
- try
- {
- HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
- ServicePointManager.DefaultConnectionLimit = int.MaxValue;
-
- myReq.Method = "POST";
- if (pm_ContentType != null)
- {
- myReq.ContentType = pm_ContentType;
- }
- if (headstr != null)
- {
- myReq.Headers.Add("Authorization", "Basic " + headstr);
- }
- byte[] bz = System.Text.Encoding.UTF8.GetBytes(pm_str);
- myReq.Timeout = 1800000;
- myReq.ContentLength = bz.Length;
- requestStream = myReq.GetRequestStream();
- requestStream.Write(bz, 0, bz.Length);
- requestStream.Close();
- requestStream = null;
-
- Response = (HttpWebResponse)myReq.GetResponse();
- reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
- string retstr = reader.ReadToEnd().Trim();
- return retstr;
- }
- catch (Exception ex)
- {
- return "{\"code\":\"0\",\"msg\":\"" + ex.Message.ToString() + "\"}";
- }
- finally
- {
- if (reader != null)
- {
- reader.Close();
- reader = null;
- }
- if (requestStream != null)
- {
- requestStream.Close();
- requestStream = null;
- }
- if (Response != null)
- {
- Response.Close();
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。