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

前端 未结 2 697
面向向阳花
面向向阳花 2021-01-21 09:14

My code so far:

FileReader fileReader = new FileReader(\"filename.xml\");
Client c = Client.create();
WebResource webResource = c.resource(\"http://localhost:808         


        
相关标签:
2条回答
  • 2021-01-21 09:41

    Have a look at the Jersey API for WebResource. It gives you a post method that accepts data.

    0 讨论(0)
  • 2021-01-21 09:42

    You can always use the java.net APIs in Java SE:

    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();
    
    0 讨论(0)
提交回复
热议问题