public string PostData(string url, int timeOut, string fileKeyName, string filePath)
{
string responseContent;
var memStream = new MemoryStream();
var webRequest = (HttpWebRequest)WebRequest.Create(url);
// 边界符
var boundary = "------------" + DateTime.Now.Ticks.ToString("x");
// 编码边界符
var firstBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");//拼接第一个form表单值用到
var encodingBoundary = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
var endBoundary = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
//本地读取方式
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
// 设置属性
webRequest.Method = "POST";
webRequest.Timeout = timeOut;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
//file表单值 filename的后缀会影响服务器保存的文件格式
const string filePartHeader =
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";
var header = string.Format(filePartHeader, fileKeyName, "123.jpg");
var headerbytes = Encoding.UTF8.GetBytes(header);
memStream.Write(encodingBoundary, 0, encodingBoundary.Length);
memStream.Write(headerbytes, 0, headerbytes.Length);
var buffer = new byte[1024];
int bytesRead; // =0
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
// 写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length);
webRequest.ContentLength = memStream.Length;
var requestStream = webRequest.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
using (var httpStreamReader = GetWebContent(httpWebResponse))
{
responseContent = httpStreamReader.ReadToEnd();
}
fileStream.Close();
httpWebResponse.Close();
webRequest.Abort();
return responseContent;
}
/// <summary>
/// 获取web请求后的返回值
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static StreamReader GetWebContent(HttpWebResponse response)
{
Stream desStream;
if (response.ContentEncoding == "gzip")
{
desStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
}
else
{
desStream = response.GetResponseStream();
}
string strcs = response.CharacterSet;
StreamReader xmlStream;
if (strcs == "")
{
xmlStream = new System.IO.StreamReader(desStream, System.Text.Encoding.Default);
}
else
{
strcs = strcs.Trim().ToLower();
switch (strcs)
{
case "gb2312":
xmlStream = new System.IO.StreamReader(desStream, System.Text.Encoding.GetEncoding("gb2312"));
break;
case "utf-8":
xmlStream = new System.IO.StreamReader(desStream, System.Text.Encoding.UTF8);
break;
default:
xmlStream = new System.IO.StreamReader(desStream, System.Text.Encoding.Default);
break;
}
}
return xmlStream;
}
public bool IsReusable
{
get
{
return false;
}
}
调用方法:?
//上传文件到文件服务器 string resultData = PostData(fileUpLoadURL, 1000000, "File", filePath + fileName);
|