content type 介绍

本篇介绍以html表单提交数据为例,讲述content-type头

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试表单提交Post</title>
</head>
<body>
    <form action="post.php" method="post">
        <input type="text" name='username'>
        <input type="text" name='password'>
        <input type="submit">
    </form>
</body>
</html>

这种最常见的表单,它的请求头为:

POST /test/httpPost/post.php HTTP/1.1 
Host: localhost 
Connection: keep-alive 
Content-Length: 27 
Cache-Control: max-age=0 
Origin: http://localhost 
Upgrade-Insecure-Requests: 1 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 
Content-Type: application/x-www-form-urlencoded 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 
Referer: http://localhost/test/httpPost/index.html 
Accept-Encoding: gzip, deflate 
Accept-Language: zh-CN,zh;q=0.8 
Cookie: pgv_pvi=8193968128

  

请求体:
username=zzzz&password=tttt
这里需要注意的是,请求体里的值是经过url encode的

另一种比较复杂的http请求是这样的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试表单提交Post</title>
</head>
<body>
    <form action="post.php" method="post" enctype="multipart/form-data">
        <input type="text" name='username'>
        <input type="text" name='password'>
        <input type="file" name='picture'>
        <input type="submit">
    </form>
</body>
</html>

申明了content-type的值为multipart/form-data

这种http post请求头为: 
POST /test/httpPost/post.php HTTP/1.1

Host: localhost 
Connection: keep-alive 
Origin: http://localhost 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 
Content-Type: multipart/form-data; boundary=—-WebKitFormBoundaryo8HMWnZkFmHAvcTX

声明为这种类型时,后面的boundary参数指定每个参数之间的分隔符 
请求体为: 
——WebKitFormBoundaryo8HMWnZkFmHAvcTX 
Content-Disposition: form-data; name=”username”

aaa 
——WebKitFormBoundaryo8HMWnZkFmHAvcTX 
Content-Disposition: form-data; name=”password”

bbb 
——WebKitFormBoundaryo8HMWnZkFmHAvcTX 
Content-Disposition: form-data; name=”picture”; filename=”1488794518kqnZP.jpg” 
Content-Type: image/jpeg

——WebKitFormBoundaryo8HMWnZkFmHAvcTX–

注意这时候的值没有经过url encode

  

原文地址:https://www.cnblogs.com/amyleell/p/9119796.html