【发布时间】:2019-04-28 05:24:28
【问题描述】:
我要打一个 API,它将返回字符串数据,并且我想发送字符串类型的数据(段落中的文本文件)。
【问题讨论】:
-
我想异步使用 apache httpClient。以前是同步的。我该怎么做?
标签: java json httpclient apache-httpclient-4.x
我要打一个 API,它将返回字符串数据,并且我想发送字符串类型的数据(段落中的文本文件)。
【问题讨论】:
标签: java json httpclient apache-httpclient-4.x
您可以使用 Apache httpcomponents,与 http entities
以下是在 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"));
【讨论】: