using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Data.Entity;
using System.Text.RegularExpressions;
using System.Net;
using System.IO.Compression;
namespace Job_DSWMS_REPORT
{
///
/// 新仓储系统 分类监管 报文上传
///
public class Job_DSWMS_REPORT_1 : IJob
{
private ILog log = LogManager.GetLogger(typeof(Job_DSWMS_REPORT_1));
private string USERCODE = "";
private string PWD = "";
private string SERVERUrl = "";
private string SERVERPORTUrl = "";
private string FilePath = "";
public void Execute(IJobExecutionContext context)
{
log.Debug($"Execute开始");
USERCODE = context.JobDetail.JobDataMap.GetString("USERCODE");
PWD = context.JobDetail.JobDataMap.GetString("PWD");
SERVERUrl = context.JobDetail.JobDataMap.GetString("SERVERUrl");
SERVERPORTUrl = context.JobDetail.JobDataMap.GetString("SERVERPORTUrl");
FilePath = context.JobDetail.JobDataMap.GetString("FilePath");
//1:调用URL指定的接口 得到返回数据 取得其中的文件名
//2:从URL+文件名中取得文件,保存至指定文件夹
try
{
var filename = getFileName();
DownLoad(filename);
}
catch (Exception ex) {
log.Error("错误中断0:" + ex.Message);
}
}
public class WMSXMLResult {
bool status { get; set; }
string code { get; set; }
public string data { get; set; }
}
private string getFileName() {
string strBuff = "";
var url = SERVERPORTUrl + "?USERCODE="+USERCODE+"&PWD="+ PWD;
var rtn = DoGet(url);
//"{\"status\":true,\"code\":null,\"message\":\"\",\"data\":\"Upload/FLJG/2021_07_06_YLDY_StockApply.FLJGRX\"}"
var jsonObj = JsonConvert.DeserializeObject(rtn);
return jsonObj.data;
}
public static string DoPost(string url, string json, int timeout = 10000)
{
try
{
string responseString = "";//post返回的结果
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.Timeout = timeout;
req.Headers.Add("Accept-Encoding", "gzip, deflate");
if (!string.IsNullOrWhiteSpace(json))
{
byte[] postBytes = Encoding.UTF8.GetBytes(json);
req.ContentType = "application/json; charset=utf-8";
req.ContentLength = Encoding.UTF8.GetByteCount(json);
Stream stream = req.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
}
else
{
req.ContentLength = 0;
}
var response = req.GetResponse();
responseString = GetResponseBody((HttpWebResponse)response);
return responseString;
}
catch (Exception e) {
return e.Message;
}
}
public static string DoGet(string url)
{
string responseString = "";//post返回的结果
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "GET";
req.ContentLength = 0;
var response = req.GetResponse();
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
responseString = streamRead.ReadToEnd();
response.Close();
streamRead.Close();
return responseString;
}
public string DownLoad(string FileName)
{
var url = SERVERUrl + FileName;
var fr = FileName.Split('/').ToList();
var fileSaveName = fr[fr.Count - 1];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
byte[] fileBytes;
using (WebResponse webRes = request.GetResponse())
{
int length = (int)webRes.ContentLength;
HttpWebResponse response = webRes as HttpWebResponse;
Stream stream = response.GetResponseStream();
var contentdisposition = response.Headers["Content-Disposition"];
var filename = fileSaveName;
//读取到内存
MemoryStream stmMemory = new MemoryStream();
byte[] buffer = new byte[length];
int i;
//将字节逐个放入到Byte中
while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
{
stmMemory.Write(buffer, 0, i);
}
fileBytes = stmMemory.ToArray();//文件流Byte
FileStream fs = new FileStream(FilePath+"\\" + filename, FileMode.OpenOrCreate);
stmMemory.WriteTo(fs);
stmMemory.Close();
fs.Close();
return FilePath + filename;
}
}
private static string GetResponseBody(HttpWebResponse response)
{
string responseBody = string.Empty;
if (response.ContentEncoding != null && response.ContentEncoding.ToLower().Contains("gzip"))
{
using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(stream))
{
responseBody = reader.ReadToEnd();
}
}
}
else if (response.ContentEncoding != null && response.ContentEncoding.ToLower().Contains("deflate"))
{
using (DeflateStream stream = new DeflateStream(
response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader =
new StreamReader(stream, Encoding.UTF8))
{
responseBody = reader.ReadToEnd();
}
}
}
else
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
responseBody = reader.ReadToEnd();
}
}
}
return responseBody;
}
}
///
/// 新仓储系统 分类监管 报文上传
///
public class Job_DSWMS_SaveDailyAmount : IJob
{
private ILog log = LogManager.GetLogger(typeof(Job_DSWMS_SaveDailyAmount));
private string USERCODE = "";
private string PWD = "";
private string SERVERUrl = "";
private string SERVERPORTUrl = "";
private string SERVERPORTUrl2 = "";
private string FilePath = "";
public void Execute(IJobExecutionContext context)
{
log.Debug($"Execute开始");
USERCODE = context.JobDetail.JobDataMap.GetString("USERCODE");
PWD = context.JobDetail.JobDataMap.GetString("PWD");
SERVERUrl = context.JobDetail.JobDataMap.GetString("SERVERUrl");
//SERVERPORTUrl = context.JobDetail.JobDataMap.GetString("SERVERPORTUrl");
SERVERPORTUrl2 = context.JobDetail.JobDataMap.GetString("SERVERPORTUrl2");
//1:调用URL指定的接口 得到返回数据 取得其中的文件名
//2:从URL+文件名中取得文件,保存至指定文件夹
try
{
var _data = SetDo();
//DownLoad(filename);
}
catch (Exception ex)
{
log.Error("错误中断0:" + ex.Message);
}
}
public class WMSXMLResult
{
bool status { get; set; }
string code { get; set; }
public string data { get; set; }
}
private string getFileName()
{
string strBuff = "";
var url = SERVERPORTUrl + "?USERCODE=" + USERCODE + "&PWD=" + PWD;
var rtn = DoGet(url);
//"{\"status\":true,\"code\":null,\"message\":\"\",\"data\":\"Upload/FLJG/2021_07_06_YLDY_StockApply.FLJGRX\"}"
var jsonObj = JsonConvert.DeserializeObject(rtn);
return jsonObj.data;
}
public static string DoPost(string url, string json, int timeout = 10000)
{
try
{
string responseString = "";//post返回的结果
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.Timeout = timeout;
req.Headers.Add("Accept-Encoding", "gzip, deflate");
if (!string.IsNullOrWhiteSpace(json))
{
byte[] postBytes = Encoding.UTF8.GetBytes(json);
req.ContentType = "application/json; charset=utf-8";
req.ContentLength = Encoding.UTF8.GetByteCount(json);
Stream stream = req.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
}
else
{
req.ContentLength = 0;
}
var response = req.GetResponse();
responseString = GetResponseBody((HttpWebResponse)response);
return responseString;
}
catch (Exception e)
{
return e.Message;
}
}
public static string DoGet(string url)
{
string responseString = "";//post返回的结果
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "GET";
req.ContentLength = 0;
var response = req.GetResponse();
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
responseString = streamRead.ReadToEnd();
response.Close();
streamRead.Close();
return responseString;
}
public string DownLoad(string FileName)
{
var url = SERVERUrl + FileName;
var fr = FileName.Split('/').ToList();
var fileSaveName = fr[fr.Count - 1];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
byte[] fileBytes;
using (WebResponse webRes = request.GetResponse())
{
int length = (int)webRes.ContentLength;
HttpWebResponse response = webRes as HttpWebResponse;
Stream stream = response.GetResponseStream();
var contentdisposition = response.Headers["Content-Disposition"];
var filename = fileSaveName;
//读取到内存
MemoryStream stmMemory = new MemoryStream();
byte[] buffer = new byte[length];
int i;
//将字节逐个放入到Byte中
while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
{
stmMemory.Write(buffer, 0, i);
}
fileBytes = stmMemory.ToArray();//文件流Byte
FileStream fs = new FileStream(FilePath + "\\" + filename, FileMode.OpenOrCreate);
stmMemory.WriteTo(fs);
stmMemory.Close();
fs.Close();
return FilePath + filename;
}
}
private string SetDo()
{
string strBuff = "";
var url = SERVERPORTUrl2 + "?USERCODE=" + USERCODE + "&PWD=" + PWD;
var rtn = DoGet(url);
//"{\"status\":true,\"code\":null,\"message\":\"\",\"data\":\"Upload/FLJG/2021_07_06_YLDY_StockApply.FLJGRX\"}"
var jsonObj = JsonConvert.DeserializeObject(rtn);
return jsonObj.data;
}
private static string GetResponseBody(HttpWebResponse response)
{
string responseBody = string.Empty;
if (response.ContentEncoding != null && response.ContentEncoding.ToLower().Contains("gzip"))
{
using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(stream))
{
responseBody = reader.ReadToEnd();
}
}
}
else if (response.ContentEncoding != null && response.ContentEncoding.ToLower().Contains("deflate"))
{
using (DeflateStream stream = new DeflateStream(
response.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader reader =
new StreamReader(stream, Encoding.UTF8))
{
responseBody = reader.ReadToEnd();
}
}
}
else
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
responseBody = reader.ReadToEnd();
}
}
}
return responseBody;
}
}
}