caiyintongload.aspx.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. using BizCom;
  2. using ICSharpCode.SharpZipLib.Zip;
  3. using Newtonsoft.Json.Linq;
  4. using SiteCore.Redis;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.IO;
  11. using System.Net.Security;
  12. using System.Net;
  13. using System.Text;
  14. using System.Web;
  15. using System.Web.UI;
  16. using System.Security.Cryptography.X509Certificates;
  17. using Utils;
  18. using System.Runtime.InteropServices;
  19. using NPOI.OpenXmlFormats.Shared;
  20. public partial class caiyingtongload : System.Web.UI.Page
  21. {
  22. public static string upPath = ConfigurationManager.AppSettings["upPath"];
  23. public static string copyPath = ConfigurationManager.AppSettings["copyPath"];
  24. public static string siteUrl = ConfigurationManager.AppSettings["OriSiteUrl"];
  25. [DllImport("DrvInterface64.dll", CharSet = CharSet.Unicode)]
  26. public static extern uint DecFile(string filename);
  27. [DllImport("DrvInterface64.dll", CharSet = CharSet.Unicode)]
  28. public static extern int IsFileEncrypted(string filename);//返回1为加密,0为未被加密
  29. [DllImport("DrvInterface64.dll", CharSet = CharSet.Ansi)]
  30. public static extern void CreateUserKey(StringBuilder key, int len);
  31. [DllImport("DrvInterface64.dll", CharSet = CharSet.Ansi)]
  32. public static extern int InitAesKey(StringBuilder key, int len);
  33. [DllImport("DrvInterface64.dll")]
  34. public static extern int IsInitedAesKey();
  35. private void conErc(string msg)
  36. {
  37. if (msg.IndexOf("访问远程主机") == -1)
  38. {
  39. XLog.SaveLog(0, msg);
  40. }
  41. Response.Write(msg);
  42. //Response.End();
  43. }
  44. private void conSuc(string msg)
  45. {
  46. Response.Write("{\"type\":\"success\",\"result\":\"" + msg + "\"}");
  47. //Response.End();
  48. }
  49. protected void Page_Load(object sender, EventArgs e)
  50. {
  51. if (!IsPostBack)
  52. {
  53. Response.Buffer = true;
  54. downloadcayingtonge();
  55. }
  56. }
  57. private string getCanDownFile(string fileName)
  58. {
  59. string[] extArr = new string[] { ".cdr", ".zip", ".rar" };
  60. foreach (string ext in extArr)
  61. {
  62. string fname = fileName + ext;
  63. if (File.Exists(fname))
  64. {
  65. return fname;
  66. }
  67. }
  68. return "";
  69. }
  70. private void updateIsDownSuccess(int userId, string ctid)
  71. {
  72. try
  73. {
  74. XLog.SaveLog(0, "updateIsDownSuccess ,ctid:" + ctid);
  75. SqlParameter[] sqlParameter ={
  76. new SqlParameter("@mainctids", SqlDbType.VarChar,500),
  77. new SqlParameter("@userid", SqlDbType.VarChar,4),
  78. new SqlParameter("@res", SqlDbType.VarChar, 4000)
  79. };
  80. sqlParameter[0].Value = ctid;
  81. sqlParameter[1].Value = userId;
  82. sqlParameter[2].Direction = ParameterDirection.Output;
  83. CeErpTradeCell.ExecuteDataSetStore("sp_set_download", sqlParameter);
  84. CeErpTradeCell.UpdateRelationOrder(ctid);
  85. }
  86. catch (Exception ex)
  87. {
  88. //errorFileLst.Add("'" + dr["ctid"].ToString() + "'");
  89. XLog.SaveLog(0, "下载发生错误,ctid:" + ctid + "," + ex.Message);
  90. }
  91. }
  92. private void downloadcayingtonge()
  93. {
  94. string tids = Request["hexdata"];
  95. int onlyDownFile = 0;//只下文件
  96. if (Request["onlyfile"] != null)
  97. {
  98. Int32.TryParse(Request["onlyfile"], out onlyDownFile);
  99. }
  100. int cyt = 0;
  101. if (Request["cyt"] != null)
  102. {
  103. Int32.TryParse(Request["cyt"], out cyt);
  104. }
  105. int userId = 0;
  106. if (Request["userid"] != null)
  107. {
  108. Int32.TryParse(Request["userid"], out userId);
  109. }
  110. String ui = "";
  111. if (cyt == 1)
  112. {
  113. StringBuilder sql = new StringBuilder();
  114. sql.AppendFormat("select ctid,seller_memo,FinishDesignTime,OrderState,SupplierName,FileMd5,IsSF from view_erptradecell where FinishDesignTime is not null and ctid in ({0}) {1}", ("'" + tids.Replace(",", "','") + "'"), ((onlyDownFile == 1) ? "" : " and OrderState=5 "));
  115. DataTable dt = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  116. if (dt == null || dt.Rows.Count < 1)
  117. {
  118. conErc("没有找到相关订单");
  119. return;
  120. }
  121. List<string> files = new List<string>();
  122. List<string> noFileLst = new List<string>();
  123. List<string> ctidLst = new List<string>();
  124. foreach (DataRow dr in dt.Rows)
  125. {
  126. string fileMd5 = "", fileNames = "";
  127. String seller_memo = "";
  128. int ISSF = 0;
  129. string finishDesignTime = getDesignTime(dr["FinishDesignTime"]);
  130. string df_name = upPath + "\\" + finishDesignTime + "\\" + formatMemo(dr["seller_memo"]);
  131. if (!Convert.IsDBNull(dr["seller_memo"]))
  132. {
  133. seller_memo = dr["seller_memo"].ToString();
  134. }
  135. if (!Convert.IsDBNull(dr["IsSF"]))
  136. {
  137. ISSF = Convert.ToInt32(dr["IsSF"] == null ? 0 : dr["IsSF"]);
  138. }
  139. string fname = getCanDownFile(df_name);
  140. if (string.IsNullOrEmpty(fname))
  141. {
  142. noFileLst.Add("'" + dr["ctid"].ToString() + "'");
  143. ui += dr["ctid"].ToString() + "未找到附件,";
  144. continue;
  145. }
  146. files.Add(fname);
  147. ctidLst.Add(dr["ctid"].ToString());
  148. fileMd5 += "," + dr["FileMd5"].ToString();
  149. fileNames += Path.GetFileName(fname);
  150. String er = downLoadFiles(userId, dr["ctid"].ToString(), fname, fileNames, seller_memo, ISSF);
  151. if (er.Contains("成功"))
  152. {
  153. copyFile(getDesignDate(dr["FinishDesignTime"]), dr["SupplierName"].ToString(), fname);
  154. }
  155. ui += er + ",";
  156. }
  157. conSuc(ui);
  158. }
  159. else
  160. {
  161. conSuc(ui);
  162. }
  163. }
  164. private void downloadMore()
  165. {
  166. if (Request["hexdata"] == null || Request["hexdata"].Trim() == "")
  167. {
  168. conErc("错误的下载访问");
  169. return;
  170. }
  171. string tids = Request["hexdata"];
  172. int onlyDownFile = 0;//只下文件
  173. if (Request["onlyfile"] != null)
  174. {
  175. Int32.TryParse(Request["onlyfile"], out onlyDownFile);
  176. }
  177. int cyt = 0;
  178. if (Request["cyt"] != null)
  179. {
  180. Int32.TryParse(Request["cyt"], out cyt);
  181. }
  182. int userId = 0;
  183. if (Request["userid"] != null)
  184. {
  185. Int32.TryParse(Request["userid"], out userId);
  186. }
  187. int mvClientDown = 0;//转移到客户端下载
  188. int isFromClient = 0;
  189. if (onlyDownFile != 1)
  190. {
  191. if (Request["supplier"] == null || Request["supplier"].ToString() != "1")//如果不是只下文件,那他就要是供应商
  192. {
  193. conErc("错误的下载访问");
  194. return;
  195. }
  196. else
  197. {
  198. Int32.TryParse(Request["isFromClient"], out isFromClient);
  199. if (userId > 0 && isFromClient != 1)
  200. {
  201. string file_client_down_flg = erpRedis.RedisHelper.StringGet("file_client_down_flg_" + userId);
  202. if (file_client_down_flg != null && file_client_down_flg == "1")
  203. {
  204. mvClientDown = 1;
  205. }
  206. }
  207. }
  208. }
  209. try
  210. {
  211. StringBuilder sql = new StringBuilder();
  212. sql.AppendFormat("select ctid,seller_memo,FinishDesignTime,OrderState,SupplierName,FileMd5,IsSF from view_erptradecell where FinishDesignTime is not null and ctid in ({0}) {1}", ("'" + tids.Replace(",", "','") + "'"), ((onlyDownFile == 1 || isFromClient == 1) ? "" : " and OrderState=5 "));
  213. DataTable dt = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  214. if (dt == null || dt.Rows.Count < 1)
  215. {
  216. conErc("没有找到相关订单");
  217. return;
  218. }
  219. string fileMd5 = "", fileNames = "";
  220. String seller_memo = "";
  221. int ISSF = 0;
  222. List<string> files = new List<string>();
  223. List<string> noFileLst = new List<string>();
  224. List<string> ctidLst = new List<string>();
  225. foreach (DataRow dr in dt.Rows)
  226. {
  227. string finishDesignTime = getDesignDate(dr["FinishDesignTime"]);
  228. string df_name = upPath + "\\" + finishDesignTime + "\\" + formatMemo(dr["seller_memo"]);
  229. if (!Convert.IsDBNull(dr["seller_memo"]))
  230. {
  231. seller_memo = dr["seller_memo"].ToString();
  232. }
  233. if (!Convert.IsDBNull(dr["IsSF"]))
  234. {
  235. ISSF = Convert.ToInt32(dr["IsSF"] == null ? 0 : dr["IsSF"]);
  236. }
  237. string fname = getCanDownFile(df_name);
  238. if (string.IsNullOrEmpty(fname))
  239. {
  240. noFileLst.Add("'" + dr["ctid"].ToString() + "'");
  241. continue;
  242. }
  243. files.Add(fname);
  244. ctidLst.Add(dr["ctid"].ToString());
  245. fileMd5 += "," + dr["FileMd5"].ToString();
  246. fileNames += "#$#" + Path.GetFileName(fname);
  247. if (onlyDownFile != 1 && mvClientDown != 1)
  248. copyFile(getDesignDate(dr["FinishDesignTime"]), dr["SupplierName"].ToString(), fname);
  249. }
  250. if (files.Count == 0)
  251. {
  252. conErc("没有找到相关的设计附件");
  253. return;
  254. }
  255. if (noFileLst.Count > 0)
  256. {
  257. string tips = "";
  258. string notids = string.Join(",", noFileLst.ToArray());
  259. sql = new StringBuilder();
  260. sql.AppendFormat("insert into CE_ErpTradeLog(tid,orderstate,userid,operatetime,con) select ctid,{1},{2},getdate(),'{3}' from ce_erptradecell where ctid in ({0}) ;", notids, (int)OrderState.下单完成, 0, "找不到设计文件");
  261. CeErpTradeCell.ExecuteNonQuery(sql.ToString());
  262. tips += "找不到文件,单号:" + notids;
  263. conErc(tips);
  264. return;
  265. }
  266. if (mvClientDown == 1)
  267. {
  268. for (int i = 0; i < files.Count; i++)
  269. {
  270. if (onlyDownFile != 1)
  271. updateIsDownSuccess(userId, ctidLst[i]);
  272. }
  273. string key = "file_client_down_list_" + userId;
  274. string data = "{\"hexdata\":\"" + tids + "\",\"fileMd5\":\"" + fileMd5.Substring(1) + "\",\"supplier\":\""
  275. + (Request["supplier"] != null ? Request["supplier"].ToString() : "0") + "\",\"fileName\":\"" + System.Web.HttpUtility.UrlEncode(fileNames, System.Text.Encoding.GetEncoding("GB2312")) + "\"}";
  276. erpRedis.RedisHelper.ListLeftPush(key, data);
  277. return;
  278. }
  279. if (files.Count == 1)
  280. {
  281. downLoadFile(userId, ctidLst[0], files[0], onlyDownFile, isFromClient);
  282. if (cyt == 1)
  283. {
  284. downLoadFiles(userId, ctidLst[0], files[0], fileNames, seller_memo, ISSF);
  285. }
  286. }
  287. else
  288. {
  289. ZipFileDownload(userId, ctidLst, files, "LT_" + DateTime.Now.ToString("yyyyMMddhhMmss") + ".zip", onlyDownFile, isFromClient); //downLoadFile(userId, ctidLst[i], files[i]);
  290. }
  291. //conSuc("文件已下载完成");
  292. return;
  293. }
  294. catch (Exception ex)
  295. {
  296. conErc("错误的下载访问" + ex.Message);
  297. return;
  298. }
  299. }
  300. private string formatMemo(object memo)
  301. {
  302. string m = memo.ToString();
  303. m = m.Replace("\\", "").Replace("/", "").Replace(":", "").Replace("*", "").Replace("?", "").Replace("<", "").Replace(">", "").Replace("|", "");
  304. return m;
  305. }
  306. private void copyFile(string date, string supplier, string file)
  307. {
  308. string path = copyPath + "\\" + date + "\\" + supplier + "\\" + "车间下载";
  309. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  310. string fname = Path.GetFileName(file);
  311. File.Copy(file, path + "\\" + fname, true);
  312. if (!File.Exists(path + "\\" + fname))
  313. {
  314. File.Copy(file, path + "\\" + fname, true);
  315. }
  316. }
  317. private string getDesignTime(object v)
  318. {
  319. if (v.ToString() == "") return "";
  320. return Convert.ToDateTime(v).ToString("yyyyMMdd");
  321. }
  322. private string getDesignDate(object v)
  323. {
  324. int year = DateTime.Now.Year;
  325. int month = DateTime.Now.Month;
  326. int day = DateTime.Now.Day;
  327. return year + "\\" + month + "\\" + day;
  328. /*if (v.ToString() == "") return "";
  329. return Convert.ToDateTime(v).ToString("yyyy-MM-dd");*/
  330. }
  331. /// 批量进行多个文件压缩到一个文件
  332. /// </summary>
  333. /// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者
  334. /// <param name="zipFileName">生成的zip文件名称</param>
  335. private void ZipFileDownload(int userId, List<string> ctidLst, List<string> files, string zipFileName, int onlyDownFile, int isFromClient)
  336. {
  337. MemoryStream ms = new MemoryStream();
  338. byte[] buffer = null;
  339. using (ZipFile file = ZipFile.Create(ms))
  340. {
  341. file.BeginUpdate();
  342. //file.NameTransform = new ZipNameTransform();
  343. file.NameTransform = new MyNameTransfom();
  344. foreach (var item in files)
  345. {
  346. if (File.Exists(item)) file.Add(item);
  347. }
  348. file.CommitUpdate();
  349. buffer = new byte[ms.Length];
  350. ms.Position = 0;
  351. ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
  352. ms.Flush();
  353. ms.Close();
  354. }
  355. Response.Clear();
  356. Response.Buffer = true;
  357. Response.ContentType = "application/x-zip-compressed";
  358. Response.AddHeader("Set-Cookie", "fileDownload=true; path=/");
  359. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(zipFileName));
  360. Response.BinaryWrite(buffer);
  361. Page.Response.Flush();
  362. for (int i = 0; i < files.Count; i++)
  363. {
  364. if (onlyDownFile != 1 && isFromClient != 1)
  365. updateIsDownSuccess(userId, ctidLst[i]);
  366. XLog.SaveLog(userId, files[i] + " is success");
  367. }
  368. }
  369. //public static object downfileObj = new object();
  370. private void downLoadFile(int userId, string ctid, string file, int onlyDownFile, int isFromClient)
  371. {
  372. XLog.SaveLog(userId, file);
  373. string filePath = file;
  374. string dfile = Path.GetFileName(file);
  375. FileStream fs = new FileStream(filePath, FileMode.Open);
  376. byte[] bytes = new byte[(int)fs.Length];
  377. fs.Read(bytes, 0, bytes.Length);
  378. fs.Close();
  379. Response.Clear();
  380. //
  381. Response.ClearContent();
  382. Response.ClearHeaders();
  383. Response.ContentType = "application/octet-stream";
  384. //通知浏览器下载文件而不是打开\\fileDownload=true; path=/
  385. Response.AddHeader("Set-Cookie", "fileDownload=true; path=/;");
  386. //Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  387. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(dfile, System.Text.Encoding.UTF8));
  388. Response.AddHeader("Content-Length", bytes.Length.ToString());
  389. Response.BinaryWrite(bytes);
  390. Response.Flush();
  391. if (onlyDownFile != 1 && isFromClient != 1)
  392. updateIsDownSuccess(userId, ctid);
  393. XLog.SaveLog(userId, file + " is success");
  394. }
  395. private String downLoadFiles(int userId, string ctid, string file, String fileNames, String seller_memo, int ISSF)
  396. {
  397. int IsFileEncrypt = IsFileEncrypted(file);
  398. if (IsFileEncrypt == 1 && File.Exists(file))
  399. {
  400. string key = "gBQnlxiBb7MthH9644V0W0pFwqYZgyy7";
  401. var sb = new StringBuilder(key);
  402. //Linux系统的接口要调用一次,Windows系统要调用两次
  403. CreateUserKey(sb, key.Length);
  404. CreateUserKey(sb, key.Length); //破解用dse生成的文件,需要调2次。
  405. int result = InitAesKey(sb, sb.Length);
  406. result = IsInitedAesKey();
  407. if (result != 1)
  408. {
  409. addLog(ctid, userId, "初始化秘钥失败" + result, 0);
  410. return ctid + "调用失败";
  411. }
  412. uint a = DecFile(file);
  413. if (a != 0)
  414. {
  415. addLog(ctid, userId, "解密失败" + a, 0);
  416. return ctid + "调用失败";
  417. }
  418. }
  419. int Isshunfen = 2;
  420. int Isjifu = 1;
  421. if (ISSF > 0)
  422. {
  423. Isshunfen = 1;
  424. }
  425. if (ISSF == 2)
  426. {
  427. Isjifu = 0;
  428. }
  429. UpLoadFile uf = new UpLoadFile();
  430. string YEAR = DateTime.Now.Year.ToString();
  431. string MONTH = DateTime.Now.Month.ToString();
  432. string DAY = DateTime.Now.Day.ToString();
  433. string strid = Guid.NewGuid().ToString("N");
  434. string fileExtension = Path.GetExtension(fileNames);//文件格式
  435. string finalfilename = strid + Path.GetExtension(fileNames);// ".cdr";
  436. string onlinepathfilename = "Uploads/temp/" + YEAR + "-" + MONTH + "-" + DAY + "/" + finalfilename;
  437. uf.uploadFilName(file, onlinepathfilename);
  438. string output = fileExtension.Replace(".", ""); // 移除点(.)
  439. CeErpTradeCell ce = new CeErpTradeCell();
  440. ce = CeErpTradeCell.GetByCtid(ctid);
  441. if (ce.ReturnReason != "")//打回先取消
  442. {
  443. try
  444. {
  445. cancelOrder(ctid);
  446. }
  447. catch (Exception ex)
  448. {
  449. addLog(ctid, userId, "订单取消失败" + ex.ToString(), 0);
  450. }
  451. }
  452. if (ce.OrderState == 5)
  453. {
  454. JObject jsonObject = new JObject
  455. {
  456. { "Userid", "77886" },
  457. { "pwd", "lt666888" },
  458. { "FileId", strid },
  459. { "ext", output },
  460. { "platformorder", ce.tid },//淘宝单号
  461. { "Filename", fileNames },
  462. { "Isshunfen", Isshunfen },
  463. { "Isjifu", Isjifu },
  464. { "LTOrderId", ce.ctid }
  465. };
  466. string response = HttpPost("http://www.kiy.cn/m-mobile/autobaojia/LTInsertNewOrder", jsonObject.ToString());
  467. JObject jsonObjects = JObject.Parse(response);
  468. string msg = jsonObjects["msg"].ToString();
  469. if (msg == "插入订单成功")
  470. {
  471. string cytMalId = jsonObjects["Data"]["CytMallId"]["Data"].ToString();
  472. string sql_pay = "INSERT INTO CE_CaiYingTongLog (ctid, ctime,type,msg) VALUES ('" + ctid + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'," + 0 + ",'" + msg + "');";
  473. CeErpTradeCell.ExecuteNonQuery(sql_pay.ToString());
  474. addLog(ctid, userId, "上传彩印通接口成功:" + msg, ce.OrderState);
  475. if (ce != null)
  476. {
  477. ce.FinishPlaceTime = DateTime.Now;
  478. ce.OrderState = 6;
  479. ce.IsReturn = 0;
  480. ce.PlaceUserId = userId;
  481. ce.SupplierOrderSn = cytMalId;
  482. ce.Update();
  483. CeErpTradeLog logenty = new CeErpTradeLog();
  484. logenty.tid = ctid;
  485. logenty.OrderState = 6;
  486. logenty.UserId = userId;
  487. logenty.OperateTime = DateTime.Now;
  488. logenty.Con = "下载设计文件";
  489. logenty.Create();
  490. }
  491. return ctid + "上传成功";
  492. }
  493. else
  494. {
  495. string sql_pay = "INSERT INTO CE_CaiYingTongLog (ctid, ctime,type,msg) VALUES ('" + ctid + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'," + 1 + ",'" + msg + "');";
  496. CeErpTradeCell.ExecuteNonQuery(sql_pay.ToString());
  497. addLog(ctid, userId, "上传彩印通接口失败:" + msg, ce.OrderState);
  498. XLog.SaveLog(userId, ctid + "订单下单接口调用失败!" + msg);
  499. return ctid + "调用失败";
  500. }
  501. }
  502. return ctid + "调用失败";
  503. }
  504. public static void cancelOrder(string ctid)
  505. {
  506. JObject jsonObject = new JObject
  507. {
  508. { "Userid", "77886" },
  509. { "pwd", "lt666888" },
  510. { "LTOrderId", ctid }
  511. };
  512. string response = HttpPost("http://www.kiy.cn/m-mobile/autobaojia/LTCancelOrder", jsonObject.ToString());
  513. }
  514. public static void addLog(string ctid, int userid, string con, int orderState = 0, int aftersaleState = 0)
  515. {
  516. CeErpTradeLog log = new CeErpTradeLog();
  517. log.tid = ctid;
  518. log.UserId = userid;
  519. log.Con = con;
  520. log.OrderState = orderState;
  521. log.AfterSaleState = aftersaleState;
  522. log.OperateTime = DateTime.Now;
  523. log.Create();
  524. }
  525. public static string HttpPost(string url, string param = null)
  526. {
  527. HttpWebRequest request;
  528. //如果是发送HTTPS请求
  529. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  530. {
  531. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  532. request = WebRequest.Create(url) as HttpWebRequest;
  533. request.ProtocolVersion = HttpVersion.Version10;
  534. }
  535. else
  536. {
  537. request = WebRequest.Create(url) as HttpWebRequest;
  538. }
  539. request.Method = "POST";
  540. request.ContentType = "application/json";
  541. request.Accept = "*/*";
  542. request.Timeout = 120000;
  543. request.AllowAutoRedirect = false;
  544. StreamWriter requestStream = null;
  545. WebResponse response = null;
  546. string responseStr = null;
  547. try
  548. {
  549. requestStream = new StreamWriter(request.GetRequestStream());
  550. requestStream.Write(param);
  551. requestStream.Close();
  552. response = request.GetResponse();
  553. if (response != null)
  554. {
  555. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  556. responseStr = reader.ReadToEnd();
  557. reader.Close();
  558. }
  559. }
  560. catch (Exception ex)
  561. {
  562. return ex.Message;
  563. }
  564. finally
  565. {
  566. request = null;
  567. requestStream = null;
  568. response = null;
  569. }
  570. return responseStr;
  571. }
  572. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  573. {
  574. return true; //总是接受
  575. }
  576. }