// <summary>
/// 上传执行 方法
/// </summary>
/// <param name="parameter">上传文件请求参数</param>
public string Execute(UploadParameterType parameter)
{
using (MemoryStream memoryStream = new MemoryStream())
{
// 1.分界线
string boundary = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")), // 分界线可以自定义参数
beginBoundary = string.Format("--{0}
", boundary),
endBoundary = string.Format("
--{0}--
", boundary);
byte[] beginBoundaryBytes = parameter.Encoding.GetBytes(beginBoundary),
endBoundaryBytes = parameter.Encoding.GetBytes(endBoundary);
// 2.组装开始分界线数据体 到内存流中
memoryStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);
// 3.组装 上传文件附加携带的参数 到内存流中
if (parameter.PostParameters != null && parameter.PostParameters.Count > 0)
{
foreach (KeyValuePair<string, string> keyValuePair in parameter.PostParameters)
{
string parameterHeaderTemplate = string.Format("Content-Disposition: form-data; name="{0}"
{1}
{2}", keyValuePair.Key, keyValuePair.Value, beginBoundary);
byte[] parameterHeaderBytes = parameter.Encoding.GetBytes(parameterHeaderTemplate);
memoryStream.Write(parameterHeaderBytes, 0, parameterHeaderBytes.Length);
}
}
//// 4.组装文件头数据体 到内存流中
//string fileHeaderTemplate = string.Format("Content-Disposition: form-data; name="{0}"; filename="{1}"
Content-Type: application/octet-stream
", parameter.FileNameKey, parameter.FileNameValue);
//byte[] fileHeaderBytes = parameter.Encoding.GetBytes(fileHeaderTemplate);
//memoryStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
// 5.组装文件流 到内存流中
byte[] buffer = new byte[1024 * 1024 * 1];
int size = parameter.UploadStream.Read(buffer, 0, buffer.Length);
while (size > 0)
{
memoryStream.Write(buffer, 0, size);
size = parameter.UploadStream.Read(buffer, 0, buffer.Length);
}
// 6.组装结束分界线数据体 到内存流中
memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
// 7.获取二进制数据
byte[] postBytes = memoryStream.ToArray();
// 8.HttpWebRequest 组装
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(parameter.Url, UriKind.RelativeOrAbsolute));
webRequest.Method = "POST";
webRequest.Timeout = 10000;
webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
webRequest.ContentLength = postBytes.Length;
if (Regex.IsMatch(parameter.Url, "^https://"))
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
}
// 9.写入上传请求数据
using (Stream requestStream = webRequest.GetRequestStream())
{
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
}
// 10.获取响应
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), parameter.Encoding))
{
string body = reader.ReadToEnd();
reader.Close();
return body;
}
}
}
}
static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
public class UploadParameterType
{
public Dictionary<string, string> PostParameters = new Dictionary<string, string>();
public MemoryStream UploadStream = new MemoryStream();
public string Url = "http://api.longtechcc.com:8081/API/MES/Interface/Connector/CollectEvent/UpLoadCollect";
public Encoding Encoding = System.Text.Encoding.UTF8;
public string FileNameKey = "test";
public string FileNameValue = "test.txt";
}
--调用
UploadParameterType dd = new UploadParameterType();
Dictionary<string, string> nv = new Dictionary<string, string>();
nv.Add("RequestJsonStr", "[{"SN":"(LT)STE - ZCKZQT2.01.0(XLX)A1X2HU2110230001","WorkStationCode":"MartinLaser","SortCode":"1"},{"SN":"(LT)STE - ZCKZQT2.01.0(XLX)A1X2HU2110230002","WorkStationCode":"MartinLaser","SortCode":"2"}]");
nv.Add("VToken", "74817C6533EFA1D04FF8D34B730F9AF6F57CE0B0ABF6FEB231BA94524A284BB21A5FD21E41775358");
nv.Add("VOrigin", "TPI");
dd.PostParameters = nv;
string test=Execute(dd);
参考网页内容网址:
https://blog.csdn.net/elie_yang/article/details/80710059