Get/Post请求
通常情况下,GET请求用于读操作,POST请求用于写操作(通常包括更新、创建和删除)
http://www.cnblogs.com/tangge/p/6230757.html#t0
1 public static class RequestHelper 2 { 3 private static readonly string ConfigString = 4 System.Configuration.ConfigurationManager.ConnectionStrings["WebApi"].ConnectionString; 5 6 /// <summary> 7 /// POST请求,有参数,返回对象T 8 /// </summary> 9 /// <typeparam name="T">泛型对象T</typeparam> 10 /// <param name="url">api请求url</param> 11 /// <param name="postDataStr">泛型对象</param> 12 /// <returns></returns> 13 public static T PostT<T>(string url, T postDataStr) 14 { 15 try 16 { 17 //将对象转为json数据 Newtonsoft.Json 18 //注意:日期类型的转换为json时,使用下面方法可避免出错 19 var str = JsonConvert.SerializeObject(postDataStr); 20 21 //创建实例 22 var request = (HttpWebRequest) WebRequest.Create(ConfigString + url); 23 request.Method = "POST"; 24 request.ContentType = "application/json;charset=UTF-8"; 25 //写入 26 var myRequestStream = request.GetRequestStream(); 27 var myStreamWriter = new StreamWriter(myRequestStream, Encoding.UTF8); 28 myStreamWriter.Write(str.ToCharArray()); 29 myStreamWriter.Flush(); 30 //读取 31 var response = (HttpWebResponse) request.GetResponse(); 32 var myResponseStream = response.GetResponseStream(); 33 var myJson = string.Empty; 34 if (myResponseStream != null) 35 using (var sr = new StreamReader(myResponseStream, Encoding.UTF8)) 36 { 37 myJson = sr.ReadToEnd(); 38 } 39 //json转为对象 40 var result = JsonConvert.DeserializeObject<T>(myJson); 41 return result; 42 } 43 catch (Exception ex) 44 { 45 throw new Exception(ex.ToString()); 46 } 47 } 48 49 /// <summary> 50 /// POST请求,有参数,返回Object 51 /// </summary> 52 /// <param name="url">api请求url</param> 53 /// <param name="postDataStr">object参数</param> 54 /// <returns></returns> 55 public static object PostObject(string url, object postDataStr) 56 { 57 try 58 { 59 //将对象转为json数据 Newtonsoft.Json 60 //注意:日期类型的转换为json时,使用下面方法避免出错 61 var str = JsonConvert.SerializeObject(postDataStr); 62 63 //创建实例 64 var request = (HttpWebRequest) WebRequest.Create(ConfigString + url); 65 request.Method = "POST"; 66 request.ContentType = "application/json;charset=UTF-8"; 67 //写入 68 var myRequestStream = request.GetRequestStream(); 69 var myStreamWriter = new StreamWriter(myRequestStream, Encoding.UTF8); 70 myStreamWriter.Write(str.ToCharArray()); 71 myStreamWriter.Flush(); 72 //读取 73 var response = (HttpWebResponse) request.GetResponse(); 74 var myResponseStream = response.GetResponseStream(); 75 var myJson = string.Empty; 76 if (myResponseStream != null) 77 using (var sr = new StreamReader(myResponseStream, Encoding.UTF8)) 78 { 79 myJson = sr.ReadToEnd(); 80 } 81 //json转为对象 82 var result = JsonConvert.DeserializeObject<object>(myJson); 83 return result; 84 } 85 catch (Exception ex) 86 { 87 throw new Exception(ex.ToString()); 88 } 89 } 90 91 /// <summary> 92 /// POST请求,返回泛型T对象 93 /// </summary> 94 /// <typeparam name="T">泛型T对象</typeparam> 95 /// <param name="url">api请求的url</param> 96 /// <param name="postDataStr">请求参数</param> 97 /// <returns></returns> 98 public static T PostString<T>(string url, string postDataStr) 99 { 100 try 101 { 102 var urlStr = ConfigString + url + "?" + postDataStr; 103 var client = new HttpClient(); 104 HttpContent content = new StringContent(postDataStr); 105 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 106 var response = client.PostAsync(urlStr, content).Result; 107 108 var myJson = response.Content.ReadAsStringAsync().Result; 109 //json转为对象 110 var result = JsonConvert.DeserializeObject<T>(myJson); 111 return result; 112 } 113 catch (Exception ex) 114 { 115 throw new Exception(ex.ToString()); 116 } 117 } 118 119 /// <summary> 120 /// POST请求,有参数,无返回值 121 /// </summary> 122 /// <param name="url">api请求的url</param> 123 /// <param name="postDataStr">请求参数</param> 124 public static void PostString(string url, string postDataStr) 125 { 126 try 127 { 128 var urlStr = ConfigString + url + "?" + postDataStr; 129 var client = new HttpClient(); 130 HttpContent content = new StringContent(postDataStr); 131 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 132 client.PostAsync(urlStr, content); 133 } 134 catch (Exception ex) 135 { 136 throw new Exception(ex.ToString()); 137 } 138 } 139 140 /// <summary> 141 /// POST请求,无参数,返回T 142 /// </summary> 143 /// <typeparam name="T">返回的T对象</typeparam> 144 /// <param name="url">api请求的url</param> 145 /// <returns></returns> 146 public static T HostPost<T>(string url) 147 { 148 try 149 { 150 var request = (HttpWebRequest) WebRequest.Create(ConfigString + url); 151 request.Method = "POST"; 152 request.ContentType = "application/json"; 153 request.ContentLength = 0; //post请求必须带参数,如果无参数请设置为0 154 155 var response = (HttpWebResponse) request.GetResponse(); 156 var myResponseStream = response.GetResponseStream(); 157 var myJson = string.Empty; 158 if (myResponseStream != null) 159 using (StreamReader sr = new StreamReader(myResponseStream, Encoding.UTF8)) 160 { 161 myJson = sr.ReadToEnd(); 162 } 163 var result = JsonConvert.DeserializeObject<T>(myJson); 164 return result; 165 } 166 catch (Exception ex) 167 { 168 throw new Exception(ex.ToString()); 169 } 170 } 171 172 /// <summary> 173 /// GET请求,无参数,返回T 174 /// </summary> 175 /// <typeparam name="T">返回的T对象</typeparam> 176 /// <param name="url">api请求的url</param> 177 /// <returns></returns> 178 public static T HostGet<T>(string url) 179 { 180 try 181 { 182 var request = (HttpWebRequest) WebRequest.Create(ConfigString + url); 183 request.Method = "GET"; 184 request.ContentType = "application/json;charset=UTF-8"; 185 186 var response = (HttpWebResponse) request.GetResponse(); 187 var myResponseStream = response.GetResponseStream(); 188 var myJson = string.Empty; 189 if (myResponseStream != null) 190 using (var sr = new StreamReader(myResponseStream, Encoding.UTF8)) 191 { 192 myJson = sr.ReadToEnd(); 193 } 194 var result = JsonConvert.DeserializeObject<T>(myJson); 195 return result; 196 } 197 catch (Exception ex) 198 { 199 throw new Exception(ex.ToString()); 200 } 201 } 202 203 /// <summary> 204 /// GET请求,有参数,返回对象T 205 /// </summary> 206 /// <typeparam name="T">返回的T对象</typeparam> 207 /// <param name="url">api请求的url</param> 208 /// <param name="objectParam">请求参数</param> 209 /// <returns></returns> 210 public static T HostGet<T>(string url, object objectParam) 211 { 212 try 213 { 214 var request = (HttpWebRequest) WebRequest.Create(ConfigString + url + "?" + objectParam); 215 request.Method = "GET"; 216 request.ContentType = "application/json;charset=UTF-8"; 217 var response = (HttpWebResponse) request.GetResponse(); 218 var myResponseStream = response.GetResponseStream(); 219 var myJson = string.Empty; 220 if (myResponseStream != null) 221 using (var sr = new StreamReader(myResponseStream, Encoding.UTF8)) 222 { 223 myJson = sr.ReadToEnd(); 224 } 225 var result = JsonConvert.DeserializeObject<T>(myJson); 226 return result; 227 } 228 catch (Exception ex) 229 { 230 throw new Exception(ex.ToString()); 231 } 232 } 233 234 /// <summary> 235 /// GET请求,有参数,返回对象object 236 /// </summary> 237 /// <param name="url">api请求的url</param> 238 /// <param name="objectParam">请求参数</param> 239 /// <returns></returns> 240 public static object GetObject(string url, object objectParam) 241 { 242 try 243 { 244 var request = (HttpWebRequest) WebRequest.Create(ConfigString + url + "?" + objectParam); 245 request.Method = "GET"; 246 request.ContentType = "application/json;charset=UTF-8"; 247 var response = (HttpWebResponse) request.GetResponse(); 248 var myResponseStream = response.GetResponseStream(); 249 var myJson = string.Empty; 250 if (myResponseStream != null) 251 using (var sr = new StreamReader(myResponseStream, Encoding.UTF8)) 252 { 253 myJson = sr.ReadToEnd(); 254 } 255 var result = JsonConvert.DeserializeObject(myJson); 256 return result; 257 } 258 catch (Exception ex) 259 { 260 throw new Exception(ex.ToString()); 261 } 262 } 263 }