杭州 网站建站,wordpress游客变用户,美食网站源代码,iis 设置网站不能访问Web自动化测试#xff08;25#xff09; HTTP 协议规定 POST 提交的数据必须放在消息主体#xff08;entity-body#xff09;中#xff0c;但协议并没有规定数据必须使用什么编码方式。常见的四种编码方式如下#xff1a; 1、application/x-www-form-urlencoded 这应该是… Web自动化测试25 HTTP 协议规定 POST 提交的数据必须放在消息主体entity-body中但协议并没有规定数据必须使用什么编码方式。常见的四种编码方式如下 1、application/x-www-form-urlencoded 这应该是最常见的 POST 提交数据的方式了。浏览器的原生 form 表单如果不设置 enctype 属性那么最终就会以 application/x-www-form-urlencoded 方式提交数据。请求类似于下面这样无关的请求头在本文中都省略掉了 POST http://www.example.com HTTP/1.1 Content-Type:
application/x-www-form-urlencoded;charsetutf-8
titletestsub%5B%5D1sub%5B%5D2sub%5B%5D3 2、multipart/form-data 这又是一个常见的 POST 数据提交的方式。我们使用表单上传文件时必须让 form 的 enctyped 等于这个值下面是示例 POST http://www.example.com HTTP/1.1
Content-Type:multipart/form-data; boundary----WebKitFormBoundaryrGKCBY7qhFd3TrwA
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; nametext
title
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; namefile; filenamechrome.png
Content-Type: image/png
PNG ... content of chrome.png ... ------WebKitFormBoundaryrGKCBY7qhFd3TrwA-- 3、application/json application/json 这个 Content-Type 作为响应头大家肯定不陌生。实际上现在越来越多的人把它作为请求头用来告诉服务端消息主体是序列化后的 JSON 字符串。由于 JSON 规范的流行除了低版本 IE 之外的各大浏览器都原生支持 JSON.stringify服务端语言也都有处理 JSON 的函数使用 JSON 不会遇上什么麻烦。 4、text/xml 它是一种使用 HTTP 作为传输协议XML 作为编码方式的远程调用规范。 那么Python在调用外部http请求时post请求怎么传请求体呢说实话楼主只实践过【1、application/x-www-form-urlencoded】【2、multipart/form-data 】和【3、application/json】 一、application/x-www-form-urlencoded import urlliburl http://www.example.com
body_value {package: com.tencent.lian,version_code: 66 } body_value urllib.urlencode(body_value) request urllib2.Request(url, body_value) request.add_header(keys, headers[keys]) result urllib2.urlopen(request ).read() 二、multipart/form-data 需要利用python的poster模块安装posterpip install poster 代码 from poster.encode import multipart_encode
from poster.streaminghttp import register_openers url http://www.example.com
body_value {package: com.tencent.lian,version_code: 66 } register_openers() datagen, re_headers multipart_encode(body_value) request urllib2.Request(url, datagen, re_headers) # 如果有请求头数据则添加请求头 request .add_header(keys, headers[keys]) result urllib2.urlopen(request ).read() 二、application/json import jsonurl http://www.example.com
body_value {package: com.tencent.lian,version_code: 66 } register_openers() body_value json.JSONEncoder().encode(body_value) request urllib2.Request(url, body_value) request .add_header(keys, headers[keys]) result urllib2.urlopen(request ).read() 转载于:https://www.cnblogs.com/111testing/p/6079565.html