How to POST NON-JSON request using Apache HttpClient?

☆樱花仙子☆ 提交于 2019-12-13 06:19:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!