POST xml data using java

后端 未结 3 794
独厮守ぢ
独厮守ぢ 2021-02-14 21:04

I have used the following java code to POST xml data to a remote url and get the response. Here, I am using an xml file as the input. What I need is to pass the xml as a string

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-14 21:59

    To get your XML file contents as a String use (add catch-block for IOException)

    StringBuilder bld = new StringBuilder();
    FileReader fileReader = new FileReader(input);
    BufferedReader reader = new BufferedReader(fileReader);
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        bld.append(line);
    }
    String xml = bld.toString();
    

    The better way is to use Java Web Services JAX-WS or Java Restful Web Services JAX-RS.

提交回复
热议问题