纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ICSSoft.Common
  9. {
  10. public class HTTPHelper
  11. {
  12. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  13. public static string HttpPost(string apiName, string url, string body)
  14. {
  15. try
  16. {
  17. //log.Debug(url + Environment.NewLine + body);
  18. Encoding encoding = Encoding.UTF8;
  19. System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  20. request.Method = "POST";
  21. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  22. request.ContentType = "application/json; charset=utf-8";
  23. // request.ContentType = "text/html, application/xhtml+xml";
  24. byte[] buffer = encoding.GetBytes(body);
  25. request.ContentLength = buffer.Length;
  26. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  27. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  28. using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
  29. {
  30. return reader.ReadToEnd();
  31. }
  32. }
  33. catch (System.Net.WebException ex)
  34. {
  35. log.Error(ex.ToString() + Environment.NewLine + url + Environment.NewLine + body);
  36. throw new Exception(apiName + "调用失败," + ex.Message);
  37. }
  38. }
  39. public static string RestFulGet(string jsonParam, string url)
  40. {
  41. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  42. Encoding encoding = Encoding.UTF8;
  43. request.Method = "GET";
  44. request.ContentType = "application/x-www-form-urlencoded";
  45. request.Credentials = CredentialCache.DefaultCredentials;
  46. request.Headers.Add("Content-Type", "application/json");
  47. request.Headers.Add("Accept", "application/json");
  48. request.Headers.Add("Authorization", "Basic TEST:12345678");
  49. byte[] buffer = encoding.GetBytes(jsonParam);
  50. request.ContentLength = buffer.Length;
  51. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  52. string str = string.Empty;
  53. WebResponse response = null;
  54. try
  55. {
  56. response = request.GetResponse();
  57. }
  58. catch (WebException ex)
  59. {
  60. if (ex.Status == WebExceptionStatus.ProtocolError)
  61. response = (WebResponse)ex.Response;
  62. }
  63. if (response != null)
  64. {
  65. using (StreamReader st = new StreamReader(response.GetResponseStream(), encoding))
  66. {
  67. str = System.Web.HttpUtility.UrlDecode(st.ReadToEnd());
  68. }
  69. }
  70. return str;
  71. }
  72. }
  73. }