如何使用Apache HttpClient发布NON-JSON请求?

问题描述 投票:0回答:1

我要点击一个返回字符串数据的API,我也想发送字符串类型的数据(段落中的文本文件)。

java json httpclient apache-httpclient-4.x
1个回答
0
投票

您可以使用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"));
© www.soinside.com 2019 - 2024. All rights reserved.