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 ...
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 ... :) –
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 –
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