C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器
使用multipart/form-data方式提交數(shù)據(jù)與普通的post方式有一定區(qū)別。multipart/form-data的請求頭必須包含一個特殊的頭信息:Content-Type,其值必須為multipart/form-data。另外還需要規(guī)定一個內(nèi)容分割符用于分割請求體中的多個post的內(nèi)容,如文件內(nèi)容和文本內(nèi)容,只有這樣服務(wù)端才能正常解析數(shù)據(jù)。但是,multipart/form-data的基礎(chǔ)還是post,它是由post方法來實現(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)文章
HttpWebRequest出錯.Section=ResponseHeader Detail=CR
HttpWebRequest出錯.Section=ResponseHeader Detail=CR...2007-03-03
詳解C#中的System.Timers.Timer定時器的使用和定時自動清理內(nèi)存應(yīng)用
這篇文章主要介紹了詳解C#中的System.Timers.Timer定時器的使用和定時自動清理內(nèi)存應(yīng)用,需要的朋友可以參考下2017-06-06
基于C#的UDP協(xié)議的同步通信實現(xiàn)代碼
本篇文章主要介紹了基于C#的UDP協(xié)議的同步實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

