2011-07-05 39 views
9

Tôi đã sử dụng mã java sau để POST dữ liệu xml vào một url từ xa và nhận phản hồi. Ở đây, tôi đang sử dụng một tệp xml làm đầu vào. Những gì tôi cần là để vượt qua xml như một chuỗi không phải là một tập tin ... là có anyway tôi có thể làm điều này? Ai đó có thể giúp tôi? Cảm ơn rất nhiều!Dữ liệu POST xml bằng cách sử dụng java

mã 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(); 
    } 
} 

    } 

CẬP NHẬT: Tôi cần phải vượt qua XML như là một chuỗi và loại bỏ sự tham gia của tập tin xml ...

Trả lời

8

Phương thức setRequestEntity trên org.apache.commons.httpclient.methods.PostMethod có phiên bản quá tải chấp nhận StringRequestEntity làm đối số. Bạn nên sử dụng điều này nếu bạn muốn chuyển dữ liệu của bạn dưới dạng chuỗi (ngược lại với luồng đầu vào). Vì vậy, mã của bạn sẽ trông giống như sau:

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

Hy vọng điều đó sẽ hữu ích.

+0

Cảm ơn rất nhiều, tôi nghĩ rằng điều này giải quyết vấn đề của tôi ... :) –

+0

công việc dint này cho tôi. vui lòng giúp câu hỏi này http://stackoverflow.com/questions/32884587/how-to-sent-xml-file-endpoint-url-in-restful-web-service/32884767#32884767 –

+0

Tôi có thể tao nhã tạo giá trị khóa XML như thế nào cặp thay vì sử dụng chuỗi thuần túy? – therealprashant

0

Để có được nội dung file XML của bạn như là một sử dụng String (thêm catch-block cho 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(); 

Cách tốt hơn là sử dụng Dịch vụ web Java JAX-WS hoặc JAX-RS của Dịch vụ Web Restful Java.

+0

Xin chào, Cảm ơn rất nhiều vì câu trả lời của bạn, tôi không cần phải liên quan đến tệp xml chút nào thay vì tôi cần chuyển xml thành chuỗi ... có cách nào để làm điều đó không? –

1

Bạn có thể chuyển đổi XML để String từ phương pháp này

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; 
} 

Thêm bạn có thể vượt qua chuỗi này như một tham số trên PostMethod như thế này.

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

Toàn bộ XML sẽ được truyền cho khách hàng trong queryString.

+0

Xin chào, Cảm ơn rất nhiều vì câu trả lời của bạn, tôi không cần phải liên quan đến tệp xml chút nào thay vì tôi cần chuyển xml thành chuỗi ... có cách nào để làm điều đó không? –

Các vấn đề liên quan