java - 如何计算 Http Post 请求的请求正文的 Content-Length
问题描述
我正在尝试计算 http post 请求的请求正文的 Content-Length,但我不断收到指示错误 Content-Length 的错误。请求正文如下所示:
Map<String, String> body = {
'grant_type': 'authorization_code',
'client_id': 'clientid',
'code': authCode,
'redirect_uri': 'http://localhost:8080/login/callback',
'code_verifier':
'codeverifier',
};
我尝试了几种解决方案,例如将正文的内容连接成一个字符串,例如以下,然后将其转换为字节数组并发送它的长度,但它不起作用。
String bodyStr = "grant_type:authorization_code" +
"client_id:clientid" +
"code:{$authCode}" +
"redirect_uri:http://localhost:8080/login/callback" +
"code_verifier:codeverifier";
List<int> bytes = utf8.encode(bodyStr);
发布请求正文是 x-www-form-urlencoded 格式,并且必须正确计算内容长度。任何帮助表示赞赏,谢谢。
解决方案
我自己封装的。一般来说,我不需要计算它。除非是特殊场合。Okhttp3 推荐
推荐阅读
- php - 包括 php 链接不起作用 [localhost] php
- c++ - 如何找到总和满足要求的子序列?
- python - 如何使用 scrapy 下载 amazon-s3 图像
- python - 获取两个标签区间内的兄弟标签内容:]tag1; tag5 [ = tag2、tag3、tag4 与 Xpath -Python -Selenium
- python - 试图为python下载openCV,但它一直给我这个错误
- javascript - 在 React 钩子中排序后如何渲染数组
- java - MongoDB聚合与spring
- c++ - 尽管使用 cap_sys_nice,但无法将线程优先级设置为实时
- azure - 无法使用 Azure 站点恢复连接到 SAP HANA 备份中的“AAD1”服务
- django - Django/Nginx - 找不到静态文件