標籤:lin pat amp ipa exce files coding text enter
/// <summary>
/// C#調用介面上傳json資料,並且帶檔案上傳
/// </summary>
/// <param name="url">介面地址</param>
/// <param name="filePath">檔案路徑</param>
/// <returns></returns>
public string RequestPost(string url, string filePath)
{
//string filePath = AppDomain.CurrentDomain.BaseDirectory + "CountData"; + aa.txt
string campaignId = "123";
string mailingId = "567";
string plateForm = "cn";
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
// E:\WEB\K11SH郵件服務V4-WP\WinServe\WinServe\bin\Debug\CountData\K11_hard_bounce.csv.gpg
//string filePath = AppDomain.CurrentDomain.BaseDirectory + "CountData" + Path.DirectorySeparatorChar.ToString() + Path.GetFileName("K11_hard_bounce.csv.gpg");
byte[] fileContentByte = new byte[1024]; // 檔案內容二進位
#region 將檔案轉成二進位
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
fileContentByte = new byte[fs.Length]; // 二進位檔案
fs.Read(fileContentByte, 0, Convert.ToInt32(fs.Length));
fs.Close();
#endregion
#region 定義請求體中的內容 並轉成二進位
string boundary = "ceshi";
string Enter = "\r\n";
string campaignIDStr = "--" + boundary + Enter
+ "Content-Disposition: form-data; name=\"campaignId\"" + Enter + Enter
+ campaignId + Enter;
string fileContentStr = "--" + boundary + Enter
+ "Content-Type:application/octet-stream" + Enter
+ "Content-Disposition: form-data; name=\"gnupgFile\"; filename=\"" + fileName + "\"" + Enter + Enter;
string updateTimeStr = Enter + "--" + boundary + Enter
+ "Content-Disposition: form-data; name=\"mailingId\"" + Enter + Enter
+ mailingId;
string encryptStr = Enter + "--" + boundary + Enter
+ "Content-Disposition: form-data; name=\"plateForm\"" + Enter + Enter
+ plateForm + Enter + "--" + boundary + "--";
byte[] campaignIDStrByte = Encoding.UTF8.GetBytes(campaignIDStr);//modelId所有字串二進位
byte[] fileContentStrByte = Encoding.UTF8.GetBytes(fileContentStr);//fileContent一些名稱等資訊的二進位(不包含檔案本身)
byte[] updateTimeStrByte = Encoding.UTF8.GetBytes(updateTimeStr);//updateTime所有字串二進位
byte[] encryptStrByte = Encoding.UTF8.GetBytes(encryptStr);//encrypt所有字串二進位
#endregion
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "multipart/form-data;boundary=" + boundary;
using (Stream myRequestStream = request.GetRequestStream())//定義請求流
{
//將各個二進位 安順序寫入請求流 modelIdStr -> (fileContentStr + fileContent) -> uodateTimeStr -> encryptStr
myRequestStream.Write(campaignIDStrByte, 0, campaignIDStrByte.Length);
myRequestStream.Write(fileContentStrByte, 0, fileContentStrByte.Length);
myRequestStream.Write(fileContentByte, 0, fileContentByte.Length);
myRequestStream.Write(updateTimeStrByte, 0, updateTimeStrByte.Length);
myRequestStream.Write(encryptStrByte, 0, encryptStrByte.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();//發送
Stream myResponseStream = response.GetResponseStream();//擷取傳回值
using (StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")))
{
string retString = UnicodeToString(myStreamReader.ReadToEnd());
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
}catch(Exception e)
{
Log.AppendError("上傳加密檔案失敗"+fileName +" :"+e.Message);
return "";
}
}
private string UnicodeToString(string srcText)
{
return Regex.Unescape(srcText);
}
C#post調用介面並上傳檔案