使用 java POST xml 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6580193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POST xml data using java
提问by Pavithra Gunasekara
I have used the following java code to POST xml data to a remote url and get the response. Here, I am using an xml file as the input. What I need is to pass the xml as a string not a file... is there anyway I can do this? Can someone help me? Thanks a lot!
我使用以下 java 代码将 xml 数据 POST 到远程 url 并获取响应。在这里,我使用一个 xml 文件作为输入。我需要的是将 xml 作为字符串而不是文件传递......无论如何我可以这样做吗?有人能帮我吗?非常感谢!
Java code
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();
}
}
}
UPDATE: I need to pass XML as a string and remove involving xml file...
更新:我需要将 XML 作为字符串传递并删除涉及 xml 文件...
采纳答案by Perception
The setRequestEntity method on org.apache.commons.httpclient.methods.PostMethod has an overloaded version that accepts StringRequestEntityas argument. You should use this if you wish to pass in your data as a string (as opposed to an input stream). So your code would look something like this:
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);
....
Hope that helps.
希望有帮助。
回答by Andreas Krueger
To get your XML file contents as a String use (add catch-block for IOException)
将 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();
The better way is to use Java Web Services JAX-WS or Java Restful Web Services JAX-RS.
更好的方法是使用 Java Web Services JAX-WS 或 Java Restful Web Services JAX-RS。
回答by Talha Ahmed Khan
You can convert the XML to String from this method
您可以从此方法将 XML 转换为字符串
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;
}
Add you can pass this string as a parameter on PostMethod
like this.
添加您可以PostMethod
像这样将此字符串作为参数传递。
PostMethod post = new PostMethod(strURL);
post.addParamter("paramName", convertXMLFileToString(strXMLFilename ) );
The whole XML will be transmitted to the client in a queryString
.
整个 XML 将以 .xml 格式传输到客户端queryString
。