亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c# 使用WebRequest實現(xiàn)多文件上傳

 更新時間:2021年03月18日 08:28:55   作者:陳開華  
這篇文章主要介紹了c# 使用WebRequest實現(xiàn)多文件上傳的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

c#中通常使用HttpWebRequest進行HTTP網(wǎng)絡(luò)請求,HttpWebRequest只對Http請求進行了最簡單的封裝。如果要利用Http協(xié)議實現(xiàn)多文件上傳,則必須使用POST方法multipart/form-data格式。為了重復(fù)使用,我封裝了幾個方法,實現(xiàn)了多參數(shù)文件上傳。

添加引用

使用WebRequest需要添加引用System.Web,否則引入出錯。

參數(shù)封裝

方便起見,我把請求參數(shù)進行了封裝,代碼如下:

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ù),可以是普通的鍵值對,也可以是文件參數(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實現(xiàn)多文件上傳的詳細內(nèi)容,更多關(guān)于c# 多文件上傳的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • c#遞歸生成XML實例

    c#遞歸生成XML實例

    這篇文章主要介紹了c#遞歸生成XML的方法,以實例形式較為詳細的介紹了C#的遞歸算法與XML操作技巧,非常具有實用價值,需要的朋友可以參考下
    2014-11-11
  • c#圖片上傳和顯示的實現(xiàn)方法

    c#圖片上傳和顯示的實現(xiàn)方法

    這篇文章主要介紹了c#實現(xiàn)圖片上傳和顯示的實現(xiàn)方法,可實現(xiàn)圖片上傳效果預(yù)覽功能,需要的朋友可以參考下。
    2016-10-10
  • C#中數(shù)組段用法實例分析

    C#中數(shù)組段用法實例分析

    這篇文章主要介紹了C#中數(shù)組段用法,實例分析了C#數(shù)組段的定義、功能及使用方法,需要的朋友可以參考下
    2015-05-05
  • c#判斷數(shù)據(jù)庫服務(wù)器是否已經(jīng)啟動的方法

    c#判斷數(shù)據(jù)庫服務(wù)器是否已經(jīng)啟動的方法

    這篇文章主要介紹了使用c#判斷數(shù)據(jù)庫服務(wù)器是否已經(jīng)啟動的方法,大家參考使用吧
    2014-01-01
  • C#簡單的通用基礎(chǔ)字典實現(xiàn)方法

    C#簡單的通用基礎(chǔ)字典實現(xiàn)方法

    這篇文章主要介紹了C#簡單的通用基礎(chǔ)字典實現(xiàn)方法,包含了字典的索引、記錄、回調(diào)與查詢等技巧,需要的朋友可以參考下
    2014-12-12
  • 利用C#開發(fā)瀏覽器擴展的全過程記錄

    利用C#開發(fā)瀏覽器擴展的全過程記錄

    做web開發(fā)的同學(xué),經(jīng)常會用到各種chrome瀏覽器插件,那么我們寄幾怎么開發(fā)一個插件呢(其實是瀏覽器擴展)?這篇文章主要給大家介紹了關(guān)于利用C#開發(fā)瀏覽器擴展的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • Unity實現(xiàn)毫秒延時回調(diào)功能

    Unity實現(xiàn)毫秒延時回調(diào)功能

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)毫秒延時回調(diào)功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • c# ArrayList的使用方法小總結(jié)

    c# ArrayList的使用方法小總結(jié)

    arraylist是接口list的實現(xiàn)類,所以在使用過程中比較推薦使用list接口來實現(xiàn),arraylist在程序開發(fā)過程中應(yīng)用非常廣泛,接下來,腳本之家的小編給大家總結(jié)了arraylist的使用,有需要的朋友可以參考下
    2015-09-09
  • C#中Task.Yield的用途深入講解

    C#中Task.Yield的用途深入講解

    這篇文章主要給大家介紹了關(guān)于C#中Task.Yield的用途的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • C#實現(xiàn)類型的比較示例詳解

    C#實現(xiàn)類型的比較示例詳解

    這篇文章主要給大家介紹了關(guān)于C#實現(xiàn)類型的比較的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論