简体   繁体   中英

How to POST NON-JSON request using Apache HttpClient?

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

You can use Apache httpcomponents, with http entities

Here is an example for sending a file in your POST request:

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);

If you want a text content, you can use StringEntity :

StringEntity myEntity = new StringEntity("something", ContentType.create("text/plain", "UTF-8"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM