C#post调用接口并上传文件

/// <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);
}

posted @ 2017-07-25 14:38  李文平  阅读(2153)  评论(0编辑  收藏  举报