[英]How to get response as XML from webService response?
I have to create a method for getting webService response as xml
.我必须创建一个方法来获取 webService 响应为
xml
。 I know how to create with Java
class but problem is getting response as xml
from webService.我知道如何使用
Java
类创建,但问题是从 webService 获取响应为xml
。
These webServices are soap based.这些网络服务是基于肥皂的。
Thanks in advance.提前致谢。
I have just solve my problem.我刚刚解决了我的问题。
HttpURLConnection
helps me. HttpURLConnection
帮助我。
The following code block show how I make poster for getting xml
response in java
like Mozilla Poster.以下代码块显示了我如何制作海报以在
java
获取xml
响应,如 Mozilla 海报。
public static void main(String[] args) {
try {
String uri = "http://test.com/IntegratedServices/IntegratedServices.asmx?op=GetUserInfo";
String postData = new XmlTest().xmlRequest("QWERTY10");
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true); // This is important. If you not set doOutput it is default value is false and throws java.net.ProtocolException: cannot write to a URLConnection exception
connection.setRequestMethod("POST"); // This is method type. If you are using GET method you can pass by url. If method post you must write
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); // it is important if you post utf-8 characters
DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); // This three lines is importy for POST method. I wrote preceding comment.
wr.write(postData.getBytes());
wr.close();
InputStream xml = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(xml));
String line = "";
String xmlResponse = "";
while ((line = reader.readLine()) != null) {
xmlResponse += line;
}
File file = new File("D://test.xml"); // If you want to write as file to local.
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(xmlResponse);
fileWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String xmlRequest(String pin) {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
+ " <soap:Body>\n"
+ " <GetUserInfo xmlns=\"http://tempuri.org/\">\n"
+ " <pin>" + pin + "</pin>\n"
+ " </GetUserInfo>\n"
+ " </soap:Body>\n"
+ "</soap:Envelope>";
}
I hope this helps who want to get xml
as response.我希望这对想要获得
xml
作为响应的人有所帮助。 Also I wrote detailed comment to my code.我还对我的代码写了详细的注释。
For soap type webservice :对于肥皂类型的网络服务:
Parsing SOAP Response in Java 用 Java 解析 SOAP 响应
For rest look at this link:休息一下,看看这个链接:
http://duckranger.com/2011/06/jaxb-without-a-schema/ http://duckranger.com/2011/06/jaxb-without-a-schema/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.