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

40 lines
1.7 KiB

3 years ago
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. //log.Debug(url + Environment.NewLine + body);
  16. Encoding encoding = Encoding.UTF8;
  17. System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  18. request.Method = "POST";
  19. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  20. request.ContentType = "application/json; charset=utf-8";
  21. // request.ContentType = "text/html, application/xhtml+xml";
  22. byte[] buffer = encoding.GetBytes(body);
  23. request.ContentLength = buffer.Length;
  24. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  25. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  26. using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
  27. {
  28. return reader.ReadToEnd();
  29. }
  30. }
  31. catch (System.Net.WebException ex)
  32. {
  33. log.Error(ex.ToString() + Environment.NewLine + url + Environment.NewLine + body);
  34. throw new Exception(apiName + "调用失败," + ex.Message);
  35. }
  36. }
  37. }
  38. }