问题
I am to hitting an API which will return string data and as well I want to send data of string type(textfile in a paragraph).
回答1:
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"));
来源:https://stackoverflow.com/questions/55887270/how-to-post-non-json-request-using-apache-httpclient