如何使用 Apache HttpClient 发布非 JSON 请求? [英] How to POST NON-JSON request using Apache HttpClient?

查看:31
本文介绍了如何使用 Apache HttpClient 发布非 JSON 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用一个 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 发布非 JSON 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆