【问题标题】:How to POST NON-JSON request using Apache HttpClient?如何使用 Apache HttpClient 发布非 JSON 请求?
【发布时间】:2019-04-28 05:24:28
【问题描述】:

我要打一个 API,它将返回字符串数据,并且我想发送字符串类型的数据(段落中的文本文件)。

【问题讨论】:

  • 我想异步使用 apache httpClient。以前是同步的。我该怎么做?

标签: java json httpclient apache-httpclient-4.x


【解决方案1】:

您可以使用 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"));

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2018-02-09
    • 1970-01-01
    • 2019-02-04
    • 2015-08-24
    相关资源
    最近更新 更多