如何使用Apache HttpClient发布NON-JSON请求? [英] How to POST NON-JSON request using Apache HttpClient?
本文介绍了如何使用Apache HttpClient发布NON-JSON请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要选择一个API,该API将返回字符串数据,并且我也想发送字符串类型的数据(段落中的文本文件).
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).
推荐答案
您可以将Apache httpcomponents与http
You can use Apache httpcomponents, with http entities
以下是在您的POST请求中发送文件的示例:
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);
如果需要文本内容,可以使用StringEntity
:
If you want a text content, you can use StringEntity
:
StringEntity myEntity = new StringEntity("something", ContentType.create("text/plain", "UTF-8"));
这篇关于如何使用Apache HttpClient发布NON-JSON请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文