2011-07-05 70 views
9

我已經使用以下java代碼將xml數據發佈到遠程URL並獲取響應。在這裏,我使用一個xml文件作爲輸入。我需要的是將xml作爲字符串傳遞而不是文件......無論如何,我可以做到這一點嗎?有人能幫我嗎?非常感謝!使用java發佈xml數據

Java代碼

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 

import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; 
import org.apache.commons.httpclient.methods.PostMethod; 

public class xmlToString { 

public static void main(String[] args) { 
    String strURL = "https://simulator.expediaquickconnect.com/connect/ar"; 
    String strXMLFilename = "xmlfile.xml"; 
    File input = new File(strXMLFilename); 
    PostMethod post = new PostMethod(strURL); 
    try { 
     post.setRequestEntity(new InputStreamRequestEntity(
       new FileInputStream(input), input.length())); 
     post.setRequestHeader("Content-type", 
       "text/xml; charset=ISO-8859-1"); 
     HttpClient httpclient = new HttpClient(); 

     int result = httpclient.executeMethod(post); 
     System.out.println("Response status code: " + result); 
     System.out.println("Response body: "); 
     System.out.println(post.getResponseBodyAsString()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     post.releaseConnection(); 
    } 
} 

    } 

更新:我需要通過XML作爲一個字符串,刪除涉及XML文件...

回答

8

org.apache.commons.httpclient.methods.PostMethod上的setRequestEntity方法有一個重載版本,它接受StringRequestEntity作爲參數。如果您希望以字符串的形式傳遞數據(而不是輸入流),則應該使用它。所以你的代碼看起來像這樣:

String xml = "whatever.your.xml.is.here"; 
PostMethod post = new PostMethod(strURL);  
try { 
    StringRequestEntity requestEntity = new StringRequestEntity(xml); 
    post.setRequestEntity(requestEntity); 
.... 

希望有幫助。

+0

非常感謝,我認爲這解決了我的問題...... :) –

+0

這個功能適合我。請幫助這個問題http://stackoverflow.com/questions/32884587/how-to-sent-xml-file-endpoint-url-in-restful-web-service/32884767#32884767 –

+0

如何優雅地創建XML鍵值而不是使用純字符串? – therealprashant

0

爲了讓您的XML文件內容作爲字符串中使用(爲IOException增加catch-block)

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(); 

更好的方法是使用Java Web服務JAX-WS或Java Restful Web服務JAX-RS。

+0

嗨,非常感謝您的回答,我根本不需要涉及xml文件,而是需要將xml作爲字符串傳遞......有沒有辦法做到這一點? –

1

您可以從這種方法

public String convertXMLFileToString(String fileName) 
{ 
    try{ 
     DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
     InputStream inputStream = new FileInputStream(new File(fileName)); 
     org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream); 
     StringWriter stw = new StringWriter(); 
     Transformer serializer = TransformerFactory.newInstance().newTransformer(); 
     serializer.transform(new DOMSource(doc), new StreamResult(stw)); 
     return stw.toString(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

XML以字符串轉換添加你可以通過這個字符串作爲PostMethod這樣的參數。

PostMethod post = new PostMethod(strURL); 
post.addParamter("paramName", convertXMLFileToString(strXMLFilename)); 

整個XML將被傳輸到客戶端queryString

+0

嗨,非常感謝您的回答,我根本不需要涉及xml文件,而是需要將xml作爲字符串傳遞......有沒有辦法做到這一點? –