C # HTTP Series 10 form enctype attribute of the form

 In ASP.NET programming frequently encountered the following code fragment, the personnel information in a form and submit to a background program saved to the server and database.

1 <form action="userManage.ashx" method="post" enctype="application/x-www-form-urlencoded">
2  名称: <input type="text" name="uname" class="uname" /><br />
3  邮件: <input type="text" name="email" class="email" /><br />
4        <input type="submit" name="submit" value="提交"/>
5 </form>

enctype attribute specifies before being sent to the server how to encode the form data.

By default, the form data is encoded as "application / x-www-form-urlencoded". That is, before being sent to the server, all characters are encoded (converted to spaces "+" plus, special symbols into ASCII HEX value).

enctype attribute value

value description
application/x-www-form-urlencoded Coding all the characters (default) before sending
multipart/form-data

Not the character encoding.

When using the form contains file upload control, you must use this value.

text/plain Spaces converted to "+" plus, but not a special character encoding.

In the " C # 8 GET and HTTP POST series comparative illustration of " HTTP describes common methods, including OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT these types. Wherein POST typically used to submit the data to the server. More details, please refer to: HTTP1.1 agreement .

HTTP protocol is  ASCII code  transmission, the establishment of standardized application layer above TCP / IP protocol. Specification HTTP request is divided into three sections: a status line, a request header, the message body. It resembles the following:

BASH<method> <request-URL> <version>
<headers>

<entity-body>

POST predetermined data protocol must be submitted on the message body (entity-body) but the protocol does not require the data to what the encoding used . In fact, the developer can decide the format of the message body, HTTP requests sent last as long as meet the above format can be.  However, the data is sent, but also successfully resolved the server makes sense. General server languages such as .NET, JAVA, PHP, Python, etc., as well as their framework, are built to automatically resolve common data format function.

The server is usually a request header (headers) in a Content-Type  be learned request message body field which is encoded, and then parses the subject.  
So it comes to data submitted POST program includes Content-Type and a message body encoding two parts.

enctype 之 application/x-www-form-urlencoded
This is the most common way of pages POST to submit data. Native browser <form> form, if not set  enctype properties, then the final data will be submitted to application / x-www-form- urlencoded manner.
1 <form id="form1" runat="server" action="UserManageHandler.ashx" method="post" enctype="application/x-www-form-urlencoded">
2  <div>
3       名称: <input type="text" name="uname" class="uname" /><br />
4       邮件: <input type="text" name="email" class="email" /><br />
5             <input type="submit" name="submit" value="提交" />
6   </div>
7 </form>

This click [submit] button, Form submit requested data, see Fiddler capture when a request is as follows (unrelated to the request header in this article are omitted):

First, Content-Type is designated as application / x-www-form- urlencoded;
secondly, the data is encoded in accordance with the submitted val1 manner key1 = & key2 = val2 of, key, and have carried out a URL val transcoding.
Most server-side languages have very good support for this approach. For example in .NET, context.Request [ "uname"] can be obtained value name, context.Request [ "email"] to get the value of the message.

Many times, when submitting data using Ajax, also use this approach.
For example JQuery (Google Inc.) and QWrap (Baidu company) of Ajax, Content-Type default value is "application / x-www-form- urlencoded; charset = utf-8 ."

enctype 之 multipart/form-data
If required post attachments form, the need to modify the attribute enctype multipart / form-data.
<form id="form1" runat="server" action="UserManageHandler.ashx" method="post" enctype="multipart/form-data">
    <div>
            名称:  <input type="text" name="uname"   class="uname" /><br/>
            邮件:  <input type="text" name="email"   class="email" /><p/>
            附件1: <input type="file" name="file1"   class="file" /><p/>
            附件2: <input type="file" name="file2"   class="file" /><p/>
            附件3: <input type="file" name="file3"   class="file" /><p/>
                   <input type="submit" name="submit" value="提交" />
     </div>
</form>
  • application/x-www-form-urlencoded 不能用于上传文件,只能提交文本,当然如果有file控件的话也只能提交文件名。
  • multipart/form-data 用于上传文件以及文本。
 方式一:只上传一个附件,.txt普通文本类型

此点击【提交】按钮,Form提交请求数据,Fiddler抓包时看到的请求如下(无关的请求头在本文中都省略掉了):

方式二:上传多个附件,一个普通文本,一个Office word文档,一个png图片

此点击【提交】按钮,Form提交请求数据,Fiddler抓包时看到的请求如下(无关的请求头在本文中都省略掉了):

(1)boundary:用于分割不同的字段,为了避免与正文内容重复。以2个横线“--”开头,最后的字段之后以2个横线“--”结束。

(2)Content-Type: 指明了数据是以 multipart/form-data 来编码。

(3)消息主体里按照字段个数又分为多个结构类似的部分,每部分都是以 --boundary 开始,紧接着是内容描述信息,然后是回车,最后是字段具体内容(文本或二进制)。如果传输的是文件,还要包含文件名和文件类型信息。消息主体最后以 --boundary-- 标示结束。

关于 multipart/form-data 的详细定义,请查看 rfc1867

这种方式一般用来上传文件,各大服务端语言对它也有着良好的支持。

上面提到的这两种 POST 数据的方式,都是浏览器原生支持的,而且现阶段标准中原生 <form> 表单也只支持这两种方式(通过 <form> 元素的 enctype 属性指定,默认为 application/x-www-form-urlencoded。)。

随着越来越多的 Web 站点,尤其是 WebApp,全部使用 Ajax 进行数据交互之后,我们完全可以定义新的数据提交方式,给开发带来更多便利。

enctype 之 text/plain
enctype 还支持 text/plain,不过用得非常少。
<form action="userManage.ashx" method="post" enctype="text/plain">
  名称: <input type="text" name="uname" class="uname" /><br />
  邮件: <input type="text" name="email" class="email" /><br />
        <input type="submit" name="submit" value="提交"/>
 </form>

此点击【提交】按钮,Form提交请求数据,Fiddler抓包时看到的请求如下(无关的请求头在本文中都省略掉了):

application/json

application/json 这个 Content-Type 作为响应头大家肯定不陌生。实际上,现在越来越多的人把它作为请求头,用来告诉服务端消息主体是序列化后的 JSON 字符串。由于 JSON 规范的流行,除了低版本 IE 之外的各大浏览器都原生支持 JSON.stringify,服务端语言也都有处理 JSON 的函数,使用 JSON 不会遇到什么问题。

postman 使用

1、form-data: 

http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件。当上传的字段是文件时,会有Content-Type来表名文件类型;content-disposition,用来说明字段的一些信息;
由于有boundary隔离,所以multipart/form-data既可以上传文件,也可以上传键值对,它采用了键值对的方式,所以可以上传多个文件。

 点击【Code】按钮,打开如下窗体

 

2、x-www-form-urlencoded:
就是application/x-www-from-urlencoded,会将表单内的数据转换为键值对

 

点击【Code】按钮,打开如下窗体

3、raw
可以上传任意格式的文本,可以上传text、json、xml、html等

 后台代码如下:

 1 public void ProcessRequest(HttpContext context)
 2 {
 3     context.Response.ContentType = "application/json"; //"text/plain";
 4 
 5     string uname = context.Request["uname"];
 6     string email = context.Request["email"];
 7 
 8     StringBuilder sbFiles = new StringBuilder();
 9     HttpFileCollection filesCollection = context.Request.Files;
10 
11     if (filesCollection != null && filesCollection.Count > 0)
12     {
13         for (var i = 0; i < filesCollection.Count; i++)
14         {
15             HttpPostedFile postedFile = filesCollection[i];
16             if (!string.IsNullOrWhiteSpace(postedFile.FileName))
17             {
18                 sbFiles.AppendLine();
19                 sbFiles.AppendLine("附件" + (i + 1));
20                 sbFiles.AppendLine("文件名称:" + postedFile.FileName);
21                 sbFiles.AppendLine("文件大小(字节):" + postedFile.ContentLength);
22                 sbFiles.AppendLine("客户端发送的文件的 MIME 内容类型:" + postedFile.ContentType);
23             }
24         }
25     }
26 
27     context.Response.Write("提交结果如下:" + Environment.NewLine +
28                              "名称:" + uname + Environment.NewLine +
29                              "邮箱:" + email + Environment.NewLine +
30                              sbFiles
31                           );
32 }

Click [submit] button to return the following results:

 4、binary

Equivalent to Content-Type: application / octet-stream, you can only upload binary data.

Commonly used to upload the file, because there is no key, so only upload one file.

 

Guess you like

Origin www.cnblogs.com/SavionZhang/p/11413314.html