2011-09-26 137 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