c# 使用WebRequest實(shí)現(xiàn)多文件上傳
c#中通常使用HttpWebRequest進(jìn)行HTTP網(wǎng)絡(luò)請(qǐng)求,HttpWebRequest只對(duì)Http請(qǐng)求進(jìn)行了最簡(jiǎn)單的封裝。如果要利用Http協(xié)議實(shí)現(xiàn)多文件上傳,則必須使用POST方法multipart/form-data格式。為了重復(fù)使用,我封裝了幾個(gè)方法,實(shí)現(xiàn)了多參數(shù)文件上傳。
添加引用
使用WebRequest需要添加引用System.Web,否則引入出錯(cuò)。
參數(shù)封裝
方便起見,我把請(qǐng)求參數(shù)進(jìn)行了封裝,代碼如下:
namespace EasyHttp.Net.Core
{
public class KeyValue
{
public string Key;
public string Value;
public string FilePath;
public string ContentType="*/*";
public KeyValue(string key, string value, string filePath, string contentType)
{
Key = key;
Value = value;
FilePath = filePath;
ContentType = contentType;
}
public KeyValue() { }
public KeyValue(string key, string value, string filePath)
{
Key = key;
Value = value;
FilePath = filePath;
}
public KeyValue(string key, string value)
{
Key = key;
Value = value;
}
}
}
KeyValue代表了廣義的參數(shù),可以是普通的鍵值對(duì),也可以是文件參數(shù)。
多文件上傳封裝
public static void ExecuteMultipartRequest(HttpWebRequest request, List<KeyValue> nvc)
{
Console.WriteLine(request.Headers);
// log.Debug(string.Format("Uploading {0} to {1}", file, url));
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest wr = request;
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
using (var rs = wr.GetRequestStream())
{
// 普通參數(shù)模板
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
//帶文件的參數(shù)模板
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
foreach (KeyValue keyValue in nvc)
{
//如果是普通參數(shù)
if (keyValue.FilePath == null)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, keyValue.Key, keyValue.Value);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
//如果是文件參數(shù),則上傳文件
else
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string header = string.Format(headerTemplate, keyValue.Key, keyValue.FilePath, keyValue.ContentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
using (var fileStream = new FileStream(keyValue.FilePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[4096];
int bytesRead = 0;
long total = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
total += bytesRead;
}
}
}
}
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
}
}
使用
static void Main(string[] args)
{
var request = WebRequest.Create("http://localhost:8080/test/upload") as HttpWebRequest;
List<KeyValue> keyValues = new List<KeyValue>();
keyValues.Add(new KeyValue("key1","param1"));
keyValues.Add(new KeyValue("key2", "param2"));
keyValues.Add(new KeyValue("file","test1.png","image/png"));
keyValues.Add(new KeyValue("file", "test2.png", "image/png"));
EasyHttp.ExecuteMultipartRequest(request,keyValues);
}
以上就是c# 使用WebRequest實(shí)現(xiàn)多文件上傳的詳細(xì)內(nèi)容,更多關(guān)于c# 多文件上傳的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C#使用TcpListener及TcpClient開發(fā)一個(gè)簡(jiǎn)單的Chat工具實(shí)例
- C#語言使用gRPC、protobuf(Google Protocol Buffers)實(shí)現(xiàn)文件傳輸功能
- C# 使用HttpClient上傳文件并附帶其他參數(shù)的步驟
- c# 用Base64實(shí)現(xiàn)文件上傳
- c# 實(shí)現(xiàn)文件上傳下載功能的實(shí)例代碼
- C# 網(wǎng)絡(luò)編程之UDP
- c# 網(wǎng)絡(luò)編程之tcp
- c# 網(wǎng)絡(luò)編程之http
- C# TcpClient網(wǎng)絡(luò)編程傳輸文件的示例
相關(guān)文章
C#中子類調(diào)用父類的實(shí)現(xiàn)方法
這篇文章主要介紹了C#中子類調(diào)用父類的實(shí)現(xiàn)方法,通過實(shí)例逐步分析了類中初始化構(gòu)造函數(shù)的執(zhí)行順序問題,有助于加深對(duì)C#面向?qū)ο蟪绦蛟O(shè)計(jì)的理解,需要的朋友可以參考下2014-09-09
C#實(shí)現(xiàn)多線程的Web代理服務(wù)器實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)多線程的Web代理服務(wù)器,涉及C#多線程代理服務(wù)器的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
C#使用DevExpress中的SplashScreenManager控件實(shí)現(xiàn)啟動(dòng)閃屏和等待信息窗口
這篇文章介紹了C#使用DevExpress中的SplashScreenManager控件實(shí)現(xiàn)啟動(dòng)閃屏和等待信息窗口的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C# wpf使用ListBox實(shí)現(xiàn)尺子控件的示例代碼
本文主要介紹了C# wpf使用ListBox實(shí)現(xiàn)尺子控件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

