C#post調用介面並上傳檔案

來源:互聯網
上載者:User

標籤: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調用介面並上傳檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.