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

C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器

 更新時(shí)間:2020年08月19日 09:25:51   作者:三五月兒  
這篇文章主要介紹了C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

使用multipart/form-data方式提交數(shù)據(jù)與普通的post方式有一定區(qū)別。multipart/form-data的請求頭必須包含一個(gè)特殊的頭信息:Content-Type,其值必須為multipart/form-data。另外還需要規(guī)定一個(gè)內(nèi)容分割符用于分割請求體中的多個(gè)post的內(nèi)容,如文件內(nèi)容和文本內(nèi)容,只有這樣服務(wù)端才能正常解析數(shù)據(jù)。但是,multipart/form-data的基礎(chǔ)還是post,它是由post方法來實(shí)現(xiàn)的。下面分別給出兩種方法提交multipart/form-data數(shù)據(jù)。

1、使用form表單提交數(shù)據(jù)

<form action="xx.php" method="post" enctype="multipart/form-data">
  <input type="text" name="uname" class="uname" /><br />
  <input type="text" name="email" class="email" /><br />
  <input type="file" name="file" class="file" /><br />
  <input type="submit" name="submit" value="提交"/>
</form> 

form表單提交數(shù)據(jù)的兩種方式。

(1)application/x-www-form-urlencoded 不能用于上傳文件,只能提交文本,當(dāng)然如果有file控件的話也只能提交文件名。
(2)multipart/form-data 用于上傳文件。

2、使用HttpClient和MultipartFormDataContent

using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
{
  client.BaseAddress = new Uri("http://localhost/WapAPIExp/");
  var fileContent1 = new ByteArrayContent(File.ReadAllBytes(@"D:\xx.jpg"));
  fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("file")
  {
    FileName = "xx.jpg"
  };
  var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes("1"));
  dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form")
  {
    Name = "type"
  };
  content.Add(fileContent1);
  content.Add(dataContent);
  var result = client.PostAsync("api/Upload", content).Result;
}

到此這篇關(guān)于C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器的文章就介紹到這了,更多相關(guān)multipart form-data post 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論