简体   繁体   English

POST请求如何构造?

[英]How are POST requests constructed?

How does a web browser construct a valid HTTP POST request from HTML source code? Web浏览器如何从HTML源代码构造有效的HTTP POST请求?

For example, the login form on https://stackexchange.com/users/login looks like: 例如, https ://stackexchange.com/users/login上的登录表单如下所示:

<form method="post" action="/affiliate/form/login/submit">
        <table class="position-table">
            <tbody><tr>
                <td class="input-td">
                    <label for="email" class="accessibility-hidden">Email</label>
                    <input class="framed-text-field edit-field-overlayed" type="text" name="email" id="email" maxlength="100" style="opacity: 0.3; z-index: 1; position: relative;">
                    <span class="form-help" style="display: none;">name@example.com</span>
                </td>
                <td class="input-td">
                    <label for="password" class="accessibility-hidden">Password</label>
                    <input class="framed-text-field edit-field-overlayed" type="password" name="password" id="password" style="opacity: 0.3; z-index: 1; position: relative;">
                    <span class="form-help" style="display: none;">Password</span>
                </td>
                <td></td>
                <td class="input-td">
                    <input type="submit" class="affiliate-button" value="Sign In">
                </td>
            </tr>
        </tbody></table>

        <input type="hidden" name="affId" value="XX">
        <input type="hidden" id="fkey" name="fkey" value="XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX">
    </form>

If I intercept the request my browser sends, I can see the full POST request to be: 如果拦截浏览器发送的请求,则可以看到完整的POST请求为:

POST /affiliate/form/login/submit HTTP/1.1
Host: openid.stackexchange.com
Connection: close
Content-Length: 86
Cache-Control: max-age=0
Origin: https://openid.stackexchange.com
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: https://openid.stackexchange.com/affiliate/form?affId=11&background=transparent&callback=https%3a%2f%2fstackexchange.com%2fusers%2fauthenticate&color=black&nonce=ZUlWWgAAAADlmwCfYbAw%2bg%3d%3d&openid.sreg.requested=email&signupByDefault=false&onLoad=signin-loaded&authCode=E8UrPSRT%2bzAs6b2nqZygJVS2cHb%2fY8TwtrlMUN3bORnP0o7IZRGIatV%2fkJ73KMX0nwrcnJdf2qBKFzJJ28czMo9GWAQesGoO4qgp6XZgulhYzlpEL4XOpTotYAVSlq5K5mFQjwTy6I4YbtJrsIyFs4yy2EgNlOe%2b7E0W%2fRVpnws%3d
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cookie: prov=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX; acct=XXXXXXXXXXXXXXXvXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; canary=X; anon=XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX

email=AzureDiamond&password=Hunter2&affId=11&fkey=XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX

How does my web browser (chrome) know to construct the POST request based on what it can see in the form HTML? 我的网络浏览器(chrome)如何基于HTML表单中的内容知道如何构造POST请求? Certain aspects are obvious, request method, target, content-length, the actual etc is all visible in the form (or entered by the user). 某些方面是显而易见的,请求方法,目标,内容长度,实际值等在表格中都是可见的(或由用户输入)。

The headers that I don't understand the construction of are: 我不了解其结构的标头是:

  • Cache-Control 缓存控制
  • Upgrade-Insecure-Requests 不安全升级请求
  • Content-Type 内容类型
  • Accept 接受
  • Refer 参考
  • Accept-Encoding 接受编码
  • Accept-Language 接受语言

Are these built into/written by the browser? 这些是浏览器内置的还是由浏览器编写的? I don't think so because when I try stripping them from the request, I get bad request errors back from stackexchange. 我不这么认为,因为当我尝试从请求中剥离它们时,我从stackexchange返回错误的请求错误。 However I can't seem to find what the browser builds them from? 但是我似乎找不到浏览器从中构建它们的依据?

The browser assumes that a data requested by a form will not be static, and therefore knows to set "Cache-Control: max-age=0". 浏览器假定表单请求的数据不是静态的,因此知道设置为“ Cache-Control:max-age = 0”。 Similar default assumptions are made by browsers for the other request headers. 浏览器针对其他请求标头做出类似的默认假设。

However, it is possible to override some defaults with <META tags: enter link description here (which I found linked to under a similar question here at StackOverflow). 但是,可以使用<META标签覆盖一些默认值: 在此处输入链接描述 (我在StackOverflow的类似问题下找到了链接描述 )。 Others are set as form attributes, like <form accept-charset="ISO-8859-1" ... 其他设置为表单属性,例如<form accept-charset="ISO-8859-1" ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM