Java - 如何在rest post方法

时间:2015-06-16 08:21:33

标签: java

我需要通过传递XML请求体来进行api调用rest POST方法。我通过VB.net实现了同样的目标。在VB.net中,我使用XElement来传递请求正文。

例如:

Dim xml As XElement = <Request xmlns="request"><ID>181</ID><Password>String content</Password><Service>service name</Service><UserName>username</UserName></Request>. 

在Java中如何传递上述XML请求体来调用rest post方法。

2 个答案:

答案 0 :(得分:2)

这是如何操作的示例代码,或者您可以找到一些Java库,例如commons-httpclient,这比这更容易。

 String xmlString = "<?xml version='1.0' encoding='gb2312'?>"
                + "<Req>"
                + "<EventContentReq>"
                + "<EventID>101</EventID >"
                + "</EventContentReq>"
                + "</Req>";

        byte[] xmlData = xmlString.getBytes();

        String urlStr = "http://124.128.62.164:7001/test";

        DataInputStream input = null;

        java.io.ByteArrayOutputStream out = null;

        try {

            URL url = new URL(urlStr);

            URLConnection urlCon = url.openConnection();

            urlCon.setDoOutput(true);
            urlCon.setDoInput(true);
            urlCon.setUseCaches(false);


            urlCon.setRequestProperty("Content-Type", "text/xml");
            urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));

            input = new DataInputStream(urlCon.getInputStream());
            out = new java.io.ByteArrayOutputStream();

            byte[] bufferByte = new byte[256];
            int l = -1;

            while ((l = input.read(bufferByte)) > -1) {
                out.write(bufferByte, 0, l);
                out.flush();
            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                out.close();

                input.close();

            } catch (Exception ex) {

            }

        }

答案 1 :(得分:1)

public static void main(String[] args) {        
        try {
            String url = "pass your url";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type",
                    "application/xml;charset=utf-8");
            String urlParameters = "<Request xmlns=\"abc\"><ID>1</ID><Password></Password></Request>";
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
            String responseStatus = con.getResponseMessage();

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            System.out.println("response:" + response.toString());


            }
        } catch (IOException e) {
            System.out.println("error" + e.getMessage());
        }
    }