如何使用 Apache HttpClient POST NON-JSON 请求?
How to POST NON-JSON request using Apache HttpClient?
我要打一个 API,它将 return 字符串数据,而且我想发送字符串类型的数据(段落中的文本文件)。
您可以使用 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"));
我要打一个 API,它将 return 字符串数据,而且我想发送字符串类型的数据(段落中的文本文件)。
您可以使用 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"));