java - 如何使用 Apache HttpClient 发布非 JSON 请求?
问题描述
我要打一个API,它将返回字符串数据,并且我想发送字符串类型的数据(段落中的文本文件)。
解决方案
您可以使用带有 http实体的 Apache httpcomponents
以下是在 POST 请求中发送文件的示例:
File file = new File("somefile.txt");
FileEntity entity = new FileEntity(file, ContentType.create("text/plain", "UTF-8"));
HttpPost httppost = new HttpPost("http://localhost/action.do");
httppost.setEntity(entity);
如果你想要一个文本内容,你可以使用StringEntity
:
StringEntity myEntity = new StringEntity("something", ContentType.create("text/plain", "UTF-8"));
推荐阅读
- python - 按 id 的计数对数据框列进行排序
- javascript - JavaScript FIzzBuzz 使用类似于 Swift 的模式匹配的元组
- python - 类中的 Python 类
- python - Pandas 合并函数,将剩余值保存在数据框中
- php - 需要从 json url 获取双嵌套比特币价格数据
- python - 在python中合并表并将列组合在一起
- python-3.x - 使用带有使用者密钥和使用者秘密的 Python3 对 API 进行身份验证
- ruby - 厨师中不存在包 SQLServer2012SP4KB4018073x64ENU.exe 的源
- java - 即使找到值,String.contains() 和 stream.of.anymatch() 也会返回 false
- html - 我可以在 html 页面上嵌入 vpython 吗?