首页 > 解决方案 > 如何在颤动的 HTTP 发布请求中发送像“this”这样的请求正文?

问题描述

我正在尝试制作一个应用程序,在该应用程序中,我基本上将应用程序设为 httpClient,它只从应用程序获取输入,然后将其发送到网站,然后解析网页并从中显示特定的文本(表格)。

所以我使用的是http.post(),通过浏览器正常用户交互的http请求是这样的:

POST /example/Directory/xxx HTTP/1.1
Host: www.example.com
Connection: keep-alive
Content-Length: 15
Cache-Control: max-age=0
Origin: http://www.example.com
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
Save-Data: on
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://example.com
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
X-Hola-Unblocker-Bext: reqid 14107: before request, send headers
X-Hola-Request-Id: 14107

请求正文是:

date=07-01-2019

我尝试在 http.post() 的 'body:' 参数中发送“date=07-01-2019”,我也尝试在 body: 参数中使用它,例如 {'date':'07-01-2019'}。

我什至尝试将 {'date':'07-01-2019'} 存储在一个变量中,然后对其使用 json.encode() 但没有运气。

timeTableFetch() async{
  String url = "http://example.com";
  var response = await http.post(Uri.encodeFull(url), headers: {"Content-Type": "application/x-www-form-urlencoded", 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}, body: "date=07-01-2019");
  var document = parse(response.body);
  var timeTable = document.getElementsByTagName("gradienttable");//this is what i want

}

所以我希望得到我将要进一步处理的那个 timeTable 变量,但我无法得到我尝试打印页面的 HTML,它显示响应即将到来,但我发送的日期无效。

我已经阅读了DartLang的文档,他们使用了这样的 body

var url = "http://example.com/whatsit/create";
http.post(url, body: {"name": "doodle", "color": "blue"});

但是在我的代码中这样做仍然不起作用

更新代码:

timeTableFetch() async{
  String url = "http://example.com";
  var response = await http.post(Uri.encodeFull(url), headers: {"Content-Type": "application/x-www-form-urlencoded", 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}, body: {'date': '07-01-2019'});
  var document = parse(response.body);
  var timeTable = document.getElementsByTagName("gradienttable");//this is what i want

}

标签: dartflutter

解决方案


推荐阅读