HttpRequestHelper.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11. namespace HH.YiDaSyncNC.Helper
  12. {
  13. public class HttpRequestHelper
  14. {
  15. public static string _Static_NCApporeApiUrl = ConfigurationManager.AppSettings["NCApporeApiUrl"];
  16. public class ApporeDataModel
  17. {
  18. public int state { get; set; }
  19. public string msg { get; set; }
  20. public string result { get; set; }
  21. }
  22. #region 销售发票审核
  23. public class ApporeNCCSOBillParamModel
  24. {
  25. public string billdate { get; set; }
  26. public string csaleinvoiceid { get; set; }
  27. public string usercode { get; set; }
  28. }
  29. public static ApporeDataModel ApporeNCCSOBill(string billNo)
  30. {
  31. var param = new ApporeNCCSOBillParamModel()
  32. {
  33. billdate = DateTime.Now.ToString("yyyy-MM-dd"),
  34. csaleinvoiceid = billNo,
  35. usercode = "OA-NC"
  36. };
  37. string apiUrl = string.Format(_Static_NCApporeApiUrl + "service/~lxbxy_ext/nc.api.lxbxy.httpservice.ApporeNCCSOBill");
  38. string result = HttpPost(apiUrl, JsonConvert.SerializeObject(param));
  39. return JsonConvert.DeserializeObject<ApporeDataModel>(result);
  40. }
  41. #endregion
  42. #region 付款单审核
  43. public class ApproveNCFKBillParamModel
  44. {
  45. public string billdate { get; set; }
  46. public string paybill { get; set; }
  47. public string usercode { get; set; }
  48. }
  49. public static ApporeDataModel ApproveNCFKBill(string billNo)
  50. {
  51. var param = new ApproveNCFKBillParamModel()
  52. {
  53. billdate = DateTime.Now.ToString("yyyy-MM-dd"),
  54. paybill = billNo,
  55. usercode = "OA-NC"
  56. };
  57. string apiUrl = string.Format(_Static_NCApporeApiUrl + "service/~lxbxy_ext/nc.api.lxbxy.httpservice.ApproveNCFKBill");
  58. string result = HttpPost(apiUrl, JsonConvert.SerializeObject(param));
  59. return JsonConvert.DeserializeObject<ApporeDataModel>(result);
  60. }
  61. #endregion
  62. #region POST请求(原始方法)
  63. public static string HttpPost(string url, string postDataStr)
  64. {
  65. try
  66. {
  67. byte[] bytes = Encoding.UTF8.GetBytes(postDataStr);
  68. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  69. var host = request.RequestUri.Scheme + "://" + request.RequestUri.Host + request.RequestUri.AbsolutePath;
  70. request.Method = "POST";
  71. request.ContentType = "application/json";
  72. request.ContentLength = bytes.Length;
  73. Stream writer = request.GetRequestStream();
  74. writer.Write(bytes, 0, bytes.Length);
  75. writer.Flush();
  76. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  77. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  78. string retString = reader.ReadToEnd();
  79. response.Close();
  80. return retString;
  81. }
  82. catch (Exception ex)
  83. {
  84. throw ex;
  85. }
  86. }
  87. #endregion
  88. }
  89. }