博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
表单发送文件及加自定义参数
阅读量:6816 次
发布时间:2019-06-26

本文共 6141 字,大约阅读时间需要 20 分钟。

/****************************************************************************************
** 作者: Eddie Xu 
** 时间: 2017/12/7 20:16:44
** 版本: V1.0.0
** CLR: 4.0.30319.42000
** GUID: c76c1823-494c-4fb3-8f3e-0d957eaa4089
** 机器名: DESKTOP-ECII567
** 描述: 尚未编写描述
****************************************************************************************/

using Manjinba.Communication.Common.Logging;

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Manjinba.Communication.Common.Utils

{
public class HttpUploadUtil
{
private ArrayList bytesArray;
private Encoding encoding = Encoding.UTF8;
private string boundary = String.Empty;

public HttpUploadUtil()

{
bytesArray = new ArrayList();
string flag = DateTime.Now.Ticks.ToString("x");
boundary = "---------------------------" + flag;
}

/// <summary>

/// 合并请求数据
/// </summary>
/// <returns></returns>
private byte[] MergeContent()
{
int length = 0;
int readLength = 0;
string endBoundary = "--" + boundary + "--\r\n";
byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);

bytesArray.Add(endBoundaryBytes);

foreach (byte[] b in bytesArray)

{
length += b.Length;
}

byte[] bytes = new byte[length];

foreach (byte[] b in bytesArray)

{
b.CopyTo(bytes, readLength);
readLength += b.Length;
}

return bytes;

}

/// <summary>

/// 上传
/// </summary>
/// <param name="requestUrl">请求url</param>
/// <param name="responseText">响应</param>
/// <returns></returns>
public bool Upload(String requestUrl, out String responseText)
{
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);

byte[] responseBytes;

byte[] bytes = MergeContent();

try

{
responseBytes = webClient.UploadData(requestUrl, bytes);
responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
return true;
}
catch (WebException ex)
{
Stream responseStream = ex.Response.GetResponseStream();
responseBytes = new byte[ex.Response.ContentLength];
responseStream.Read(responseBytes, 0, responseBytes.Length);
}
responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
return false;
}
/// <summary>
/// 上传
/// </summary>
/// <param name="requestUrl">请求url</param>
/// <param name="responseText">响应</param>
/// <returns></returns>
public bool Upload(String requestUrl, long? rangeS, long? rangeE, out String responseText)
{
//WebClient webClient = new WebClient();
var webRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
try
{
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
//webRequest.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
if (rangeS != null && rangeE != null)
webRequest.AddRange(rangeS ?? 0, rangeE ?? 0);

//byte[] responseBytes;

byte[] bytes = MergeContent();
//responseBytes = webClient.UploadData(requestUrl, bytes);
webRequest.ContentLength = bytes.Length;
webRequest.Method = "POST";
using (Stream stream = webRequest.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
responseText = streamReader.ReadToEnd();
return true;
}
catch (Exception e)
{
LogHelper.GetLog().Error(e.Message + e.StackTrace);
}
//catch (WebException ex)
//{
// Stream responseStream = ex.Response.GetResponseStream();
// responseBytes = new byte[ex.Response.ContentLength];
// responseStream.Read(responseBytes, 0, responseBytes.Length);
//}
//responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
responseText = string.Empty;
return false;
}

/// <summary>

/// 设置表单数据字段
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="fieldValue">字段值</param>
/// <returns></returns>
public void SetFieldValue(String fieldName, String fieldValue)
{
string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
string httpRowData = String.Format(httpRow, fieldName, fieldValue);

bytesArray.Add(encoding.GetBytes(httpRowData));

}

/// <summary>

/// 设置表单文件数据
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="filename">字段值</param>
/// <param name="contentType">内容内型</param>
/// <param name="fileBytes">文件字节流</param>
/// <returns></returns>
public void SetFieldValue(String fieldName, String filename, String contentType, Byte[] fileBytes)
{
string end = "\r\n";
string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string httpRowData = String.Format(httpRow, fieldName, filename, contentType);

byte[] headerBytes = encoding.GetBytes(httpRowData);

byte[] endBytes = encoding.GetBytes(end);
byte[] fileDataBytes = new byte[headerBytes.Length + fileBytes.Length + endBytes.Length];

headerBytes.CopyTo(fileDataBytes, 0);

fileBytes.CopyTo(fileDataBytes, headerBytes.Length);
endBytes.CopyTo(fileDataBytes, headerBytes.Length + fileBytes.Length);

bytesArray.Add(fileDataBytes);

}

/// <summary>

/// 设置表单文件数据
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="filename">字段值</param>
/// <param name="contentType">内容内型</param>
/// <param name="fileBytes">文件字节流</param>
/// <returns></returns>
public void SetFieldValue(String fieldName, String filename, String contentType, Byte[] fileBytes, int partNum, long blocksize)
{
string end = "\r\n";
string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; blocknum={3}; blocksize={4}; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string httpRowData = String.Format(httpRow, fieldName, filename, contentType, partNum, blocksize);

byte[] headerBytes = encoding.GetBytes(httpRowData);

byte[] endBytes = encoding.GetBytes(end);
byte[] fileDataBytes = new byte[headerBytes.Length + fileBytes.Length + endBytes.Length];

headerBytes.CopyTo(fileDataBytes, 0);

fileBytes.CopyTo(fileDataBytes, headerBytes.Length);
endBytes.CopyTo(fileDataBytes, headerBytes.Length + fileBytes.Length);

bytesArray.Add(fileDataBytes);

}
}
}

转载于:https://www.cnblogs.com/Nine4Cool/p/10540652.html

你可能感兴趣的文章
spark启动简单脚本
查看>>
centos6.5中安装htop进程管理监控工具
查看>>
juniper基本配置命令 自用
查看>>
hadoop学习笔记之--- HDFS原理学习
查看>>
ThinkPHP 学习笔记(四) ThinkPHP的配置
查看>>
win32 UNICODE 支持
查看>>
MySQL+DRBD+Corosync+Pacemaker CentOS6.5版
查看>>
在CentOS 6.5上安装和配Xen
查看>>
重载类的 new,delete,new[],delete[] 运算符成员函数
查看>>
Express 3.x升级到4.x 优缺点
查看>>
我的友情链接
查看>>
inittab文件丢失恢复
查看>>
ocjp 51-60
查看>>
我的友情链接
查看>>
windows下的任务不能自动执行的解决办法
查看>>
VACL配置说明
查看>>
shell防DDOS
查看>>
go语言 学习笔记1
查看>>
一键包安装lamp或lnmp环境
查看>>
网络提速(最短路)
查看>>