2011-09-26 128 views
1

除了FORMPARAM之外,还有其他方式可以将XML文件发送到RESTful Web服务吗?JAVA :: RESTful Web服务使用XML文件

我的要求是开发一个Web服务,它使用XML文件,将其存储在本地计算机中,并返回一条声明文件已下载/保存的语句。

+0

你可以走了SOAP :) – Kris

+0

REST风格WID篮网是实际需求! – Harsha

+0

退房http://stackoverflow.com/questions/1725315/how-to-get-full-rest-request-body-using-jersey/1773308#1773308 - 它就像*不*拥有'@一样简单* FormParam'注解:'@POST public void store(String xml){...' –

回答

2

这里的发布,方式比SOAP更简单的代码...

// POST the XML string as text/xml via HTTPS 
public static String postRequest(String strRequest, String strURL) throws Exception { 
    String responseXML = null; 

    try { 
     URL url = new URL(strURL); 
     URLConnection connection = url.openConnection(); 
     HttpURLConnection httpConn = (HttpURLConnection) connection; 

     byte[] requestXML = strRequest.getBytes(); 

     // Set the appropriate HTTP parameters. 
     httpConn.setRequestProperty("Content-Length", String.valueOf(requestXML.length)); 
     httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); 
     httpConn.setRequestMethod("POST"); 
     httpConn.setDoOutput(true); 
     httpConn.setDoInput(true); 

     // Send the String that was read into postByte. 
     OutputStream out = httpConn.getOutputStream(); 
     out.write(requestXML); 
     out.close(); 

     // Read the response and write it to standard out. 
     InputStreamReader isr = new InputStreamReader(httpConn.getInputStream()); 
     BufferedReader br = new BufferedReader(isr); 
     String temp; 
     String tempResponse = ""; 

     //Create a string using response from web services 
     while ((temp = br.readLine()) != null) 
      tempResponse = tempResponse + temp; 
     responseXML = tempResponse; 
     br.close(); 
     isr.close(); 
    } catch (java.net.MalformedURLException e) { 
     System.out.println("Error in postRequest(): Secure Service Required"); 
    } catch (Exception e) { 
     System.out.println("Error in postRequest(): " + e.getMessage()); 
    } 
    return responseXML; 
} 
+0

它在哪里消耗我的XML文件? – Harsha