繁体   English   中英

Jersey REST 客户端:如何将 XML 文件添加到 POST 请求的正文中?

[英]Jersey REST Client: How to add XML file to the body of POST request?

到目前为止我的代码:

FileReader fileReader = new FileReader("filename.xml");
Client c = Client.create();
WebResource webResource = c.resource("http://localhost:8080/api/resource");
webResource.type("application/xml");

我想使用POST方法发送filename.xml的内容,但我不知道如何将它们添加到请求正文中。 我需要帮助,因为在网上我只能找到如何添加Form参数。

提前致谢。

查看WebResource的 Jersey API 它为您提供了一个接受数据的post方法。

您始终可以使用 Java SE 中的 java.net API:

URL url = new URL("http://localhost:8080/api/resource");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");

OutputStream os = connection.getOutputStream();

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
FileReader fileReader = new FileReader("filename.xml");
StreamSource source = new StreamSource(fileReader);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);

os.flush();
connection.getResponseCode();
connection.disconnect();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM