我已經使用以下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文件...
非常感謝,我認爲這解決了我的問題...... :) –
這個功能適合我。請幫助這個問題http://stackoverflow.com/questions/32884587/how-to-sent-xml-file-endpoint-url-in-restful-web-service/32884767#32884767 –
如何優雅地創建XML鍵值而不是使用純字符串? – therealprashant