纽威
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.

39 lines
1.7 KiB

3 years ago
3 years ago
3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ICSSoft.Common
  7. {
  8. public class HTTPHelper
  9. {
  10. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  11. public static string HttpPost(string apiName, string url, string body)
  12. {
  13. try
  14. {
  15. Encoding encoding = Encoding.UTF8;
  16. System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  17. request.Method = "POST";
  18. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  19. request.ContentType = "application/json; charset=utf-8";
  20. // request.ContentType = "text/html, application/xhtml+xml";
  21. byte[] buffer = encoding.GetBytes(body);
  22. request.ContentLength = buffer.Length;
  23. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  24. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  25. using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
  26. {
  27. return reader.ReadToEnd();
  28. }
  29. }
  30. catch (System.Net.WebException ex)
  31. {
  32. log.Error(ex.ToString() + Environment.NewLine + url + Environment.NewLine + body);
  33. throw new Exception(apiName + "调用失败," + ex.Message);
  34. }
  35. }
  36. }
  37. }