星火微课系统客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HttpHelper.cs 26KB

il y a 4 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Security;
  8. using System.Security.Cryptography.X509Certificates;
  9. using System.Text;
  10. using System.Threading;
  11. namespace Common.system
  12. {
  13. /// <summary>
  14. /// HttpHelper
  15. /// 创建人:赵耀
  16. /// </summary>
  17. public class HttpHelper
  18. {
  19. public static void init_Request(ref System.Net.HttpWebRequest request)
  20. {
  21. request.Accept = "text/json,*/*;q=0.5";
  22. request.Headers.Add("Accept-Charset", "utf-8;q=0.7,*;q=0.7");
  23. request.Headers.Add("Accept-Encoding", "gzip, deflate, x-gzip, identity; q=0.9");
  24. request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
  25. request.Timeout = 8000;
  26. }
  27. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  28. {
  29. return true; //总是接受
  30. }
  31. public static System.Net.HttpWebRequest GetHttpWebRequest(string url)
  32. {
  33. HttpWebRequest request = null;
  34. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  35. {
  36. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  37. request = WebRequest.Create(url) as HttpWebRequest;
  38. request.ProtocolVersion = HttpVersion.Version10;
  39. }
  40. else
  41. {
  42. request = WebRequest.Create(url) as HttpWebRequest;
  43. }
  44. return request;
  45. }
  46. public static WebResponse Download(string downloadUrl, long from, long to, string method)
  47. {
  48. for (int i = 0; i < 10; i++)
  49. {
  50. try
  51. {
  52. var request = HttpHelper.GetHttpWebRequest(downloadUrl);
  53. HttpHelper.init_Request(ref request);
  54. request.Accept = "text/json,*/*;q=0.5";
  55. request.AddRange(from, to);
  56. request.Headers.Add("Accept-Charset", "utf-8;q=0.7,*;q=0.7");
  57. request.Headers.Add("Accept-Encoding", "gzip, deflate, x-gzip, identity; q=0.9");
  58. request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
  59. request.Timeout = 120000;
  60. request.Method = method;
  61. request.KeepAlive = false;
  62. request.ContentType = "application/json; charset=utf-8";
  63. return request.GetResponse();
  64. }
  65. catch (Exception)
  66. {
  67. Thread.Sleep(100);
  68. }
  69. }
  70. throw new Exception("已断开网络!请检查网络连接后重试下载!");
  71. }
  72. public static string Get(string url, IDictionary<string, string> param)
  73. {
  74. var paramBuilder = new List<string>();
  75. foreach (var item in param)
  76. {
  77. paramBuilder.Add(string.Format("{0}={1}", item.Key, item.Value));
  78. }
  79. url = string.Format("{0}?{1}", url.TrimEnd('?'), string.Join(",", paramBuilder.ToArray()));
  80. return Get(url);
  81. }
  82. public static string Get(string url)
  83. {
  84. try
  85. {
  86. var request = GetHttpWebRequest(url);
  87. if (request != null)
  88. {
  89. string retval = null;
  90. init_Request(ref request);
  91. using (var Response = request.GetResponse())
  92. {
  93. using (var reader = new System.IO.StreamReader(Response.GetResponseStream(), System.Text.Encoding.UTF8))
  94. {
  95. retval = reader.ReadToEnd();
  96. }
  97. }
  98. return retval;
  99. }
  100. }
  101. catch
  102. {
  103. }
  104. return null;
  105. }
  106. public static string Post(string url, string data)
  107. {
  108. try
  109. {
  110. HttpWebRequest request = GetHttpWebRequest(url);
  111. if (request != null)
  112. {
  113. string retval = null;
  114. init_Request(ref request);
  115. request.Method = "POST";
  116. request.ServicePoint.Expect100Continue = false;
  117. request.ContentType = "application/json; charset=utf-8";
  118. request.Timeout = 5000;
  119. var bytes = System.Text.UTF8Encoding.UTF8.GetBytes(data);
  120. request.ContentLength = bytes.Length;
  121. using (var stream = request.GetRequestStream())
  122. {
  123. stream.Write(bytes, 0, bytes.Length);
  124. }
  125. using (var response = request.GetResponse())
  126. {
  127. using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
  128. {
  129. retval = reader.ReadToEnd();
  130. }
  131. }
  132. return retval;
  133. }
  134. }
  135. catch
  136. {
  137. }
  138. return null;
  139. }
  140. /// <summary>
  141. ///调用Post接口
  142. /// </summary>
  143. /// <param name="request"></param>
  144. /// <returns></returns>
  145. public static string HttpPost(string body, string Url)
  146. {
  147. try
  148. {
  149. Encoding encoding = Encoding.UTF8;
  150. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  151. request.Method = "POST";
  152. request.Accept = "text/html,application/xhtml+xml,*/*";
  153. request.ContentType = "application/json";
  154. byte[] buffer = encoding.GetBytes(body);
  155. request.ContentLength = buffer.Length;
  156. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  157. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  158. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  159. {
  160. return reader.ReadToEnd();
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. // LogHelper.Instance.Debug($"POST接口连接失败,请求参数:{ex.Message}");
  166. return "POST报错:" + ex.Message;
  167. }
  168. }
  169. /// <summary>
  170. /// Post Http请求
  171. /// </summary>
  172. /// <param name="url">请求地址</param>
  173. /// <param name="postData">传输数据</param>
  174. /// <param name="timeout">超时时间</param>
  175. /// <param name="contentType">媒体格式</param>
  176. /// <param name="encode">编码</param>
  177. /// <returns>泛型集合</returns>
  178. public static T PostAndRespSignle<T>(string url, int timeout = 5000, string postData = "", string contentType = "application/json;", string encode = "UTF-8") where T : class
  179. {
  180. if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null)
  181. {
  182. // webRequest.Headers.Add("Authorization", "Bearer " + "SportApiAuthData");
  183. HttpWebResponse webResponse = null;
  184. Stream responseStream = null;
  185. Stream requestStream = null;
  186. StreamReader streamReader = null;
  187. try
  188. {
  189. string respstr = GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType);
  190. return JsonHelper.JsonToObj<T>(respstr);
  191. }
  192. catch (Exception ex)
  193. {
  194. }
  195. finally
  196. {
  197. if (responseStream != null)
  198. {
  199. responseStream.Dispose();
  200. }
  201. if (webResponse != null)
  202. {
  203. webResponse.Close();
  204. }
  205. if (requestStream != null)
  206. {
  207. requestStream.Dispose();
  208. }
  209. if (streamReader != null)
  210. {
  211. streamReader.Dispose();
  212. }
  213. }
  214. }
  215. return default(T);
  216. }
  217. private static string GetStreamReader(string url, Stream responseStream, Stream requestStream, StreamReader streamReader, WebResponse webResponse, int timeout, string encode, string postData, string contentType)
  218. {
  219. try
  220. {
  221. byte[] data = Encoding.GetEncoding(encode).GetBytes(postData);
  222. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
  223. webRequest.Method = "POST";
  224. webRequest.ContentType = contentType + ";" + encode;
  225. webRequest.ContentLength = data.Length;
  226. webRequest.Timeout = timeout;
  227. requestStream = webRequest.GetRequestStream();
  228. requestStream.Write(data, 0, data.Length);
  229. webResponse = (HttpWebResponse)webRequest.GetResponse();
  230. responseStream = webResponse.GetResponseStream();
  231. if (responseStream == null) { return ""; }
  232. streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode));
  233. return streamReader.ReadToEnd();
  234. }
  235. catch (Exception ex)
  236. {
  237. return null;
  238. }
  239. }
  240. /// <summary>
  241. /// Cookie
  242. /// </summary>
  243. public static CookieContainer cc = new CookieContainer();
  244. /// <summary>
  245. /// Get请求
  246. /// </summary>
  247. /// <param name="serviceaddress">请求地址</param>
  248. /// <param name="strcontent">头</param>
  249. /// <param name="contenttype">参数</param>
  250. /// <param name="header">token</param>
  251. /// <returns></returns>
  252. public static JObject GetFunction(string serviceaddress, string strcontent, string contenttype, string header, int Timeout = 5000)
  253. {
  254. try
  255. {
  256. string serviceAddress = serviceaddress + strcontent;
  257. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
  258. WebHeaderCollection myWebHeaderCollection = request.Headers;
  259. request.CookieContainer = cc;
  260. if (header != "")
  261. {
  262. myWebHeaderCollection.Add("access_token:" + header);
  263. }
  264. request.Method = "GET";
  265. request.ContentType = contenttype;
  266. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  267. string encoding = response.ContentEncoding;
  268. if (encoding == null || encoding.Length < 1)
  269. {
  270. encoding = "UTF-8"; //默认编码
  271. }
  272. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
  273. string retString = reader.ReadToEnd();
  274. //解析josn
  275. JObject jo = JObject.Parse(retString);
  276. return jo;
  277. }
  278. catch (Exception)
  279. {
  280. return null;
  281. }
  282. }
  283. /// <summary>
  284. /// Post请求
  285. /// </summary>
  286. /// <param name="serviceaddress">请求地址</param>
  287. /// <param name="contenttype">头 application/x-www-form-urlencoded</param>
  288. /// <param name="strcontent">参数</param>
  289. /// <param name="header">token</param>
  290. /// <returns></returns>
  291. public static JObject PostFunction(string serviceaddress, string contenttype, string strcontent, string header, int Timeout = 5000)
  292. {
  293. try
  294. {
  295. string serviceAddress = serviceaddress;
  296. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
  297. WebHeaderCollection myWebHeaderCollection = request.Headers;
  298. request.CookieContainer = cc;
  299. //myWebHeaderCollection.Add("Accept", "*/*");
  300. if (header != "")
  301. {
  302. myWebHeaderCollection.Add("access_token:" + header);
  303. }
  304. request.Timeout = Timeout;
  305. request.Method = "POST";
  306. request.ContentType = contenttype;
  307. string strContent = strcontent;
  308. using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
  309. {
  310. dataStream.Write(strContent);
  311. dataStream.Close();
  312. }
  313. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  314. string encoding = response.ContentEncoding;
  315. if (encoding == null || encoding.Length < 1)
  316. {
  317. encoding = "UTF-8"; //默认编码
  318. }
  319. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
  320. string retString = reader.ReadToEnd();
  321. retString = retString.Replace("\\r\\n", "");
  322. //解析josn
  323. JObject jo = JObject.Parse(retString);
  324. return jo;
  325. }
  326. catch (Exception ex)
  327. {
  328. LogHelper.WriteErrLog("请求失败(若网络无问题,请重置winsock,解决方法:以管理员方式打开cmd执行 netsh winsock reset 后重启)", ex);
  329. return null;
  330. }
  331. }
  332. /// <summary>
  333. /// 上传方法
  334. /// </summary>
  335. /// <param name="url">webapi地址</param>
  336. /// <param name="files">本地文件路径,单文件默认为string[0]</param>
  337. /// <param name="token"></param>
  338. /// <param name="formFields">参数可不传</param>
  339. /// <returns></returns>
  340. public static JObject UploadFilesToRemoteUrl(string url, string[] files, string token, NameValueCollection formFields = null)
  341. {
  342. string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
  343. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  344. request.ContentType = "multipart/form-data; boundary=" +
  345. boundary;
  346. request.Method = "POST";
  347. request.KeepAlive = true;
  348. Stream memStream = new System.IO.MemoryStream();
  349. var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
  350. boundary + "\r\n");
  351. var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
  352. boundary + "--");
  353. WebHeaderCollection myWebHeaderCollection = request.Headers;
  354. request.CookieContainer = cc;
  355. if (token != "")
  356. {
  357. myWebHeaderCollection.Add("access_token:" + token);
  358. }
  359. string formdataTemplate = "\r\n--" + boundary +
  360. "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
  361. if (formFields != null)
  362. {
  363. foreach (string key in formFields.Keys)
  364. {
  365. string formitem = string.Format(formdataTemplate, key, formFields[key]);
  366. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  367. memStream.Write(formitembytes, 0, formitembytes.Length);
  368. }
  369. }
  370. string headerTemplate =
  371. "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
  372. "Content-Type: application/octet-stream\r\n\r\n";
  373. for (int i = 0; i < files.Length; i++)
  374. {
  375. memStream.Write(boundarybytes, 0, boundarybytes.Length);
  376. var header = string.Format(headerTemplate, "file", files[i]);
  377. var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  378. memStream.Write(headerbytes, 0, headerbytes.Length);
  379. using (var fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
  380. {
  381. var buffer = new byte[1024];
  382. var bytesRead = 0;
  383. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  384. {
  385. memStream.Write(buffer, 0, bytesRead);
  386. }
  387. }
  388. }
  389. //memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  390. request.ContentLength = memStream.Length;
  391. using (Stream requestStream = request.GetRequestStream())
  392. {
  393. memStream.Position = 0;
  394. byte[] tempBuffer = new byte[memStream.Length];
  395. memStream.Read(tempBuffer, 0, tempBuffer.Length);
  396. memStream.Close();
  397. requestStream.Write(tempBuffer, 0, tempBuffer.Length);
  398. }
  399. using (var response = request.GetResponse())
  400. {
  401. Stream stream2 = response.GetResponseStream();
  402. StreamReader reader2 = new StreamReader(stream2);
  403. JObject a = JObject.Parse(reader2.ReadToEnd());
  404. return a;
  405. }
  406. }
  407. /// <summary>
  408. /// HttpWebRequest 下载
  409. /// </summary>
  410. /// <param name="url">URI</param>
  411. /// <returns></returns>
  412. public static bool GetDataGetHtml(string url, string filePath, string header)
  413. {
  414. try
  415. {
  416. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  417. //httpWebRequest.ContentType = "application/x-www-form-urlencoded";
  418. httpWebRequest.Method = "GET";
  419. //httpWebRequest.ContentType = "image/jpeg";
  420. //对发送的数据不使用缓存
  421. httpWebRequest.AllowWriteStreamBuffering = false;
  422. //httpWebRequest.Timeout = 300000;
  423. httpWebRequest.ServicePoint.Expect100Continue = false;
  424. WebHeaderCollection myWebHeaderCollection = httpWebRequest.Headers;
  425. //myWebHeaderCollection.Add("Accept", "*/*");
  426. if (header != "")
  427. {
  428. myWebHeaderCollection.Add("access_token:" + header);
  429. }
  430. HttpWebResponse webRespon = (HttpWebResponse)httpWebRequest.GetResponse();
  431. Stream webStream = webRespon.GetResponseStream();
  432. long length = webRespon.ContentLength;
  433. if (webStream == null)
  434. {
  435. return false;
  436. }
  437. int lengthint = Convert.ToInt32(length);
  438. //int num = lengthint / 1024;
  439. //int count = 0;
  440. //M_DownloadInfo.totalCount = lengthint / 1024;
  441. //M_DownloadInfo.downloadCount = 0;
  442. Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
  443. byte[] bArr = new byte[1024];
  444. int size = webStream.Read(bArr, 0, bArr.Length);
  445. while (size > 0)
  446. {
  447. stream.Write(bArr, 0, size);
  448. size = webStream.Read(bArr, 0, bArr.Length);
  449. //M_DownloadInfo.downloadCount = M_DownloadInfo.downloadCount + 1;
  450. }
  451. webRespon.Close();
  452. stream.Close();
  453. webStream.Close();
  454. return true;
  455. }
  456. catch (Exception ex)
  457. {
  458. //LogHelper.WriteErrLog("请求失败:" + ex.Message, ex);
  459. return false;
  460. }
  461. }
  462. /// <summary>
  463. /// 上传文件流
  464. /// 创建人:赵耀
  465. /// 创建时间:2020年9月5日
  466. /// </summary>
  467. /// <param name="url">URL</param>
  468. /// <param name="databyte">文件数据流</param>
  469. /// <param name="filename">文件名</param>
  470. /// <param name="formFields">参数 可不传</param>
  471. public static JObject UploadRequestflow(string url, byte[] databyte, string filename, NameValueCollection formFields = null)
  472. {
  473. JObject jobResult = null;
  474. // 时间戳,用做boundary
  475. string boundary = "-----" + DateTime.Now.Ticks.ToString("x");
  476. byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
  477. //根据uri创建HttpWebRequest对象
  478. HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
  479. httpReq.ContentType = "multipart/form-data; boundary=" + boundary;
  480. httpReq.Method = "POST";
  481. httpReq.KeepAlive = true;
  482. //httpReq.Timeout = 300000;
  483. //httpReq.AllowWriteStreamBuffering = false; //对发送的数据不使用缓存
  484. httpReq.Credentials = CredentialCache.DefaultCredentials;
  485. try
  486. {
  487. Stream postStream = httpReq.GetRequestStream();
  488. //参数
  489. const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
  490. if (formFields != null)
  491. {
  492. foreach (string key in formFields.Keys)
  493. {
  494. postStream.Write(boundarybytes, 0, boundarybytes.Length);
  495. string formitem = string.Format(formdataTemplate, key, formFields[key]);
  496. byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
  497. postStream.Write(formitembytes, 0, formitembytes.Length);
  498. }
  499. }
  500. postStream.Write(boundarybytes, 0, boundarybytes.Length);
  501. const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
  502. //文件头
  503. string header = string.Format(headerTemplate, "file", filename);
  504. byte[] headerbytes = Encoding.UTF8.GetBytes(header);
  505. postStream.Write(headerbytes, 0, headerbytes.Length);
  506. //文件流
  507. postStream.Write(databyte, 0, databyte.Length);
  508. //结束边界
  509. byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
  510. postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
  511. postStream.Close();
  512. //获取服务器端的响应
  513. using (HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse())
  514. {
  515. Stream receiveStream = response.GetResponseStream();
  516. StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
  517. string returnValue = readStream.ReadToEnd();
  518. jobResult = JObject.Parse(returnValue);
  519. response.Close();
  520. readStream.Close();
  521. //MessageWindow.Show(returnValue);
  522. LogHelper.WriteInfoLog("【文件上传】" + returnValue);
  523. }
  524. return jobResult;
  525. }
  526. catch (Exception ex)
  527. {
  528. LogHelper.WriteErrLog("【文件上传】" + ex.Message, ex);
  529. return null;
  530. }
  531. finally
  532. {
  533. httpReq = null;
  534. }
  535. }
  536. /// <summary>
  537. /// Post Http请求
  538. /// </summary>
  539. /// <param name="url"></param>
  540. /// <param name="postData"></param>
  541. /// <param name="timeout"></param>
  542. /// <param name="contentType"></param>
  543. /// <param name="encode"></param>
  544. /// <returns>响应流字符串</returns>
  545. public static string PostAndRespStr(string url, string postData = "", int timeout = 5000, string contentType = "application/json;", string encode = "UTF-8")
  546. {
  547. if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null)
  548. {
  549. HttpWebResponse webResponse = null;
  550. Stream responseStream = null;
  551. Stream requestStream = null;
  552. StreamReader streamReader = null;
  553. try
  554. {
  555. return GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType);
  556. }
  557. catch (Exception)
  558. {
  559. }
  560. finally
  561. {
  562. if (responseStream != null)
  563. {
  564. responseStream.Dispose();
  565. }
  566. if (webResponse != null)
  567. {
  568. webResponse.Close();
  569. }
  570. if (requestStream != null)
  571. {
  572. requestStream.Dispose();
  573. }
  574. if (streamReader != null)
  575. {
  576. streamReader.Dispose();
  577. }
  578. }
  579. }
  580. return null;
  581. }
  582. }
  583. }