簡體   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