256 lines
11 KiB
C#
256 lines
11 KiB
C#
using System;
|
||
using System.Data;
|
||
using System.Configuration;
|
||
using System.Web;
|
||
using System.Web.Security;
|
||
using System.Web.UI;
|
||
using System.Web.UI.WebControls;
|
||
using System.Web.UI.WebControls.WebParts;
|
||
using System.Web.UI.HtmlControls;
|
||
using System.Security.Cryptography.X509Certificates;
|
||
using System.Net.Security;
|
||
using System.IO;
|
||
using GThinking.N_MISPRO.Data;
|
||
using GThinking.N_MISPRO.Utility;
|
||
using GThinking.N_MISPRO.MISPROAS;
|
||
using System.Net;
|
||
using System.Text;
|
||
using GThinking.N_MISPROAS;
|
||
|
||
namespace GThinking.N_XLXSBJ
|
||
{
|
||
public class SendFJ
|
||
{
|
||
|
||
public static void UploadFile(string sBJDID)
|
||
{
|
||
|
||
WriteLog("报价单附件发送", "报价单号:" + sBJDID);
|
||
//BIP必须变量
|
||
string accessToken = string.Empty;
|
||
string businessType = "yonbip-scm-scmmp%2BYonSuite";
|
||
string businessId = string.Empty;
|
||
bool usePrivateBucket = false;
|
||
|
||
string strPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "work\\temp\\BJDFJ");//金思维temp目录
|
||
if (!Directory.Exists(strPath))
|
||
Directory.CreateDirectory(strPath);
|
||
|
||
TDatabase dberp = TContextDatabases.GetDatabase("DBERP");
|
||
TSQL SQL = new TSQL(dberp);
|
||
|
||
SQL.Clear();
|
||
SQL.Add("select JKZHPZ_TOKEN from JKZHPZ WHERE JKZHPZ_RZZH='BIP' ");
|
||
DataSet dsBIP = dberp.OpenDataSet(SQL);
|
||
if (dsBIP.Tables[0].Rows.Count > 0)
|
||
{
|
||
accessToken = dsBIP.Tables[0].Rows[0]["JKZHPZ_TOKEN"].ToString().Trim();
|
||
}
|
||
WriteLog("报价单附件发送", "accessToken:" + accessToken);
|
||
if (!String.IsNullOrEmpty(accessToken))
|
||
{
|
||
SQL.Clear();
|
||
SQL.Add("SELECT HLXTDZZ_DFDZZ from HLXTDZZ where HLXTDZZ_DZLXID='12' and HLXTDZZ_DZZ=:pBJDID ");
|
||
SQL.ParamByName("pBJDID").Value = sBJDID;
|
||
DataSet dsDZZ = dberp.OpenDataSet(SQL);
|
||
if (dsDZZ.Tables[0].Rows.Count > 0)
|
||
{
|
||
businessId = dsDZZ.Tables[0].Rows[0]["HLXTDZZ_DFDZZ"].ToString().Trim();
|
||
}
|
||
WriteLog("报价单附件发送", "businessId:" + businessId);
|
||
double GRPID = 0;
|
||
string sWJMC = string.Empty;
|
||
SQL.Clear();
|
||
SQL.Add("select distinct convert(char(10),XLXSBJD_DOCGRPID) as WJID, ");
|
||
SQL.Add(" rtrim(WDWJ_MC)||'.'||WDWJ_KZM as WJMC ");
|
||
SQL.Add("from WDSL,WDWJ,XLXSBJD ");
|
||
SQL.Add("where WDSL_WJID=WDWJ_WJID and XLXSBJD_DOCGRPID=WDSL_FZID ");
|
||
SQL.Add("AND XLXSBJD_BJDID=:pBJDID ");
|
||
SQL.ParamByName("pBJDID").Value = sBJDID;
|
||
DataSet ds = dberp.OpenDataSet(SQL);
|
||
if (ds.Tables[0].Rows.Count > 0)
|
||
{
|
||
GRPID = CMISproConvert.StrToFloat(ds.Tables[0].Rows[0]["WJID"].ToString().Trim());
|
||
sWJMC = ds.Tables[0].Rows[0]["WJMC"].ToString().Trim();
|
||
}
|
||
if (GRPID > 0)
|
||
{
|
||
DocFileInfo[] arrayWDSX = FileService.GetFilesInfo(GRPID);
|
||
if (arrayWDSX.Length > 0)
|
||
{
|
||
for (int j = 0; j < arrayWDSX.Length; j++)
|
||
{
|
||
double WJID = arrayWDSX[j].FileId;
|
||
string fPath = strPath + "\\" + arrayWDSX[j].FileName;
|
||
FileService.GetFile(WJID, fPath);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 遍历下载目录,输出文件路径并删除文件
|
||
if (Directory.Exists(strPath))
|
||
{
|
||
WriteLog("报价单附件发送", "准备发送,文件目录:" + strPath);
|
||
string[] downloadedFiles = Directory.GetFiles(strPath);
|
||
foreach (string filePath in downloadedFiles)
|
||
{
|
||
WriteLog("报价单附件发送", "文件名及其位置:" + filePath);
|
||
|
||
try
|
||
{
|
||
// 强制TLS 1.2(.NET 2.0兼容写法)
|
||
try
|
||
{
|
||
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
|
||
}
|
||
catch
|
||
{
|
||
// 如果系统不支持TLS 1.2,直接抛出异常
|
||
throw new Exception("当前系统不支持TLS 1.2协议,无法建立安全连接");
|
||
}
|
||
// 跳过证书验证(测试环境使用)
|
||
ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||
{
|
||
return true;
|
||
};
|
||
|
||
// 1. 文件校验
|
||
string fullRealPath = filePath;
|
||
if (!File.Exists(fullRealPath))
|
||
{
|
||
WriteLog("报价单附件发送", "文件不存在:" + fullRealPath);
|
||
continue;
|
||
}
|
||
|
||
string fileName = Path.GetFileName(fullRealPath);
|
||
byte[] fileBytes = File.ReadAllBytes(fullRealPath);
|
||
WriteLog("报价单附件发送", "文件大小:" + fileBytes.Length + " 字节");
|
||
|
||
// 2. 拼接Query参数
|
||
StringBuilder querySb = new StringBuilder();
|
||
querySb.Append("access_token=");
|
||
querySb.Append(HttpUtility.UrlEncode(accessToken));
|
||
querySb.Append("&businessType=");
|
||
querySb.Append(HttpUtility.UrlEncode(businessType));
|
||
querySb.Append("&businessId=");
|
||
querySb.Append(HttpUtility.UrlEncode(businessId));
|
||
querySb.Append("&usePrivateBucket=");
|
||
querySb.Append(usePrivateBucket.ToString().ToLower());
|
||
|
||
string baseUrl = "https://bip-sit.hnhlxl.cn:9443/iuap-api-gateway/yonbip/uspace/iuap-apcom-file/rest/v1/file";
|
||
string fullUrl = baseUrl + "?" + querySb.ToString();
|
||
|
||
// 3. 构建multipart/form-data请求体(.NET 2.0兼容写法)
|
||
string boundary = "----YonBipUploadBoundary" + DateTime.Now.Ticks.ToString();
|
||
MemoryStream ms = new MemoryStream();
|
||
|
||
// 写入文件段头部
|
||
string header = "--" + boundary + "\r\n";
|
||
header += "Content-Disposition: form-data; name=\"files\"; filename=\"" + fileName + "\"\r\n";
|
||
header += "Content-Type: application/octet-stream\r\n";
|
||
header += "\r\n";
|
||
byte[] headerBytes = Encoding.UTF8.GetBytes(header);
|
||
ms.Write(headerBytes, 0, headerBytes.Length);
|
||
|
||
// 写入文件二进制
|
||
ms.Write(fileBytes, 0, fileBytes.Length);
|
||
|
||
// 写入结束分隔符
|
||
string footer = "\r\n--" + boundary + "--\r\n";
|
||
byte[] footerBytes = Encoding.UTF8.GetBytes(footer);
|
||
ms.Write(footerBytes, 0, footerBytes.Length);
|
||
|
||
byte[] postData = ms.ToArray();
|
||
ms.Close();
|
||
|
||
// 4. 发送请求
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl);
|
||
request.Method = "POST";
|
||
request.ContentType = "multipart/form-data; boundary=" + boundary;
|
||
request.ContentLength = postData.Length;
|
||
request.Timeout = 60000; // 60秒超时
|
||
|
||
// 写入请求体
|
||
using (Stream requestStream = request.GetRequestStream())
|
||
{
|
||
requestStream.Write(postData, 0, postData.Length);
|
||
}
|
||
|
||
// 获取响应
|
||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||
{
|
||
using (Stream responseStream = response.GetResponseStream())
|
||
{
|
||
byte[] buffer = new byte[4096];
|
||
StringBuilder resultBuilder = new StringBuilder();
|
||
int bytesRead = 0;
|
||
do
|
||
{
|
||
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
|
||
if (bytesRead > 0)
|
||
{
|
||
resultBuilder.Append(Encoding.UTF8.GetString(buffer, 0, bytesRead));
|
||
}
|
||
} while (bytesRead > 0);
|
||
|
||
string resultJson = resultBuilder.ToString();
|
||
WriteLog("报价单附件发送", "上传成功,响应:" + resultJson);
|
||
}
|
||
}
|
||
|
||
// 上传成功后删除文件
|
||
File.Delete(filePath);
|
||
WriteLog("报价单附件发送", "文件已删除:" + filePath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
WriteLog("报价单附件发送", "发送失败,文件名:" + filePath + ",错误:" + ex.Message);
|
||
if (ex.InnerException != null)
|
||
{
|
||
WriteLog("报价单附件发送", "内部错误:" + ex.InnerException.Message);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
#region 低版本兼容:证书校验静态方法(不能用Lambda)
|
||
/// <summary>
|
||
/// 测试环境忽略SSL证书校验,兼容.NET3.5/4.0
|
||
/// </summary>
|
||
private static bool CheckServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||
{
|
||
// SIT测试环境直接返回true跳过证书校验;生产删除此段,自行判断sslPolicyErrors
|
||
return true;
|
||
}
|
||
#endregion
|
||
|
||
#region 记录日志
|
||
|
||
private static void WriteLog(string sYWName, string sLogText)
|
||
{
|
||
try
|
||
{
|
||
if (!Directory.Exists(AppConfig.WorkPath))
|
||
{
|
||
DirectoryInfo dirInfo = Directory.CreateDirectory(AppConfig.WorkPath);
|
||
}
|
||
string filename = WorkDirectory.CombineLogPath(sYWName + "_" + DateTime.Now.ToString("yyyyMM") + ".log");
|
||
if (!File.Exists(filename))
|
||
{
|
||
using (File.Create(filename))
|
||
{ }
|
||
}
|
||
|
||
File.AppendAllText(filename, DateTime.Now.ToString() + sLogText + "\r\n");
|
||
|
||
}
|
||
catch { };
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|