HttpWebRequestHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using Newtonsoft.Json;
  7. namespace WebAPIBase.Utils
  8. {
  9. /// <summary>
  10. /// 使用HttpWebRequest远程访问
  11. /// </summary>
  12. public class HttpWebRequestHelper
  13. {
  14. public static string RequestPost(string url, List<Dictionary<string, string>> requestdata)
  15. {
  16. try
  17. {
  18. string requestBody = "";
  19. byte[] postData = null;
  20. if (requestdata != null)
  21. {
  22. requestBody = JsonConvert.SerializeObject(requestdata);
  23. //using (Stream postStream = new MemoryStream())
  24. //{
  25. // byte[] postData = Encoding.ASCII.GetBytes(requestBody);
  26. // postStream.Write(postData, 0, postData.Length);
  27. //}
  28. postData = Encoding.UTF8.GetBytes(requestBody);
  29. }
  30. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  31. req.ContentType = "application/json";
  32. //req.MediaType = "application/json";
  33. //req.Accept = "application/json";
  34. req.Method = "POST";
  35. req.ContentLength = postData == null ? 0 : postData.Length;
  36. //WebHeaderCollection headers = new WebHeaderCollection();
  37. //headers.Add("Authorization: Token" + authtoken);
  38. //httpWebRequest.Headers = headers;
  39. if (!string.IsNullOrEmpty(requestBody))
  40. {
  41. using (Stream postStream = req.GetRequestStream())
  42. {
  43. postStream.Write(postData, 0, postData.Length);
  44. }
  45. }
  46. HttpWebResponse res = (HttpWebResponse)req.GetResponse(); // Fails on this line.
  47. using (StreamReader streamReader = new StreamReader(res.GetResponseStream()))
  48. {
  49. string json = streamReader.ReadToEnd();
  50. //LogHelper.Info("request json:" + json);
  51. return json;
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. throw;
  57. }
  58. }
  59. public static string RequestGet(string url, List<Dictionary<string, string>> requestdata)
  60. {
  61. var loginName = "";
  62. var pwd = "";
  63. foreach(var item in requestdata)
  64. {
  65. if(item.ContainsKey("loginName"))
  66. {
  67. loginName = item["loginName"];
  68. }
  69. if(item.ContainsKey("pwd"))
  70. {
  71. pwd = item["pwd"];
  72. }
  73. }
  74. url = url + "?loginName=" + loginName + "&pwd=" + pwd;
  75. NLog.LogManager.GetCurrentClassLogger().Info("url:" + url);
  76. try
  77. {
  78. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  79. //req.ContentType = "application/json";
  80. //req.MediaType = "application/json";
  81. //req.Accept = "application/json";
  82. req.Method = "GET";
  83. HttpWebResponse res = (HttpWebResponse)req.GetResponse(); // Fails on this line.
  84. using (StreamReader streamReader = new StreamReader(res.GetResponseStream()))
  85. {
  86. string json = streamReader.ReadToEnd();
  87. //LogHelper.Info("request json:" + json);
  88. return json;
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. throw;
  94. }
  95. }
  96. }
  97. }