我要点击一个返回字符串数据的API,我也想发送字符串类型的数据(段落中的文本文件)。
您可以使用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"));