[英]How receive xml response from SOAP endpoint?
My problem is that I have one endpoint url which if I put to the browser I can see the website. 我的问题是我有一个端点网址,如果我放到浏览器,我可以看到网站。 When I use this endpoint in postman program with correct request (xml) I am receiving xml response. 当我在postman程序中使用此端点并且请求正确(xml)时,我正在接收xml响应。 Which is what I want. 这就是我想要的。 But I am trying to receive the same response in my java application but what I am receiving is the website(html). 但我试图在我的Java应用程序中收到相同的响应,但我收到的是网站(html)。 I am not sure what is wrong 我不确定是什么问题
JAXBContext jaxbContext = JAXBContext.newInstance(Envelope.class);
Marshaller jaxbMarshaler = jaxbContext.createMarshaller();
jaxbMarshaler.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Date now = new Date();
SimpleDateFormat formater = new SimpleDateFormat("ddMMyyhhmmss");
fileName = "EMCS_" + tNumber.trim() + "_" + formater.format(now) + "_" + obtDto.getTransactionIdHost()
+ ".in";
jaxbMarshaler.marshal(envelope, new File("C:\\Temp\\" + fileName));
url = new URL(urlString);
connection = (HttpsURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("SOAPAction", "");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Authorization",
"Basic " + Base64.encode("USER:PASSWORD".getBytes()));
connection.setRequestProperty("Content-Type", "text/xml");
BufferedOutputStream dos = new BufferedOutputStream(connection.getOutputStream());
File file = new File("C:\\Temp\\" + fileName);
BufferedInputStream fileOutput = new BufferedInputStream(new FileInputStream(file));
byte[] b = new byte[(int) file.length()];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) fileOutput.read();
System.out.printf((char) b[i] + "");
}
dos.write(b);
dos.flush();
dos.close();
fileOutput.close();
System.out.println("RESPONSE: " + connection.getResponseCode());
BufferedOutputStream responseWebsite = new BufferedOutputStream(
new FileOutputStream("C:\\Temp\\response.html"));
InputStream in = url.openStream(); // IMPORTANT TO READ PROPERLY DATA
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result2 = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseWebsite.write(line.getBytes());
result2.append(line + "\n");
}
responseWebsite.close();
By using code above I am posting request and getting HTML response instead of XML. 通过使用上面的代码,我发布请求并获取HTML响应而不是XML。 What I am doing wrong? 我做错了什么?
EDIT I edited post cause I need give more explanation I think. 编辑我编辑的帖子因为我需要给出更多的解释。 My problem is that in 'reader' variable bytes which I am receiving are HTML not XML. 我的问题是,我收到的'reader'变量字节是HTML而不是XML。 I want XML. 我想要XML。
EDIT2 EDIT2
So I can not give a link to repo cause is not me private project. 所以我不能给出回购原因的链接不是我的私人项目。 But when I am using postman and sending request to endpoint with 但是当我使用邮递员并向端点发送请求时
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:de.aeb.xnsg.emcs.bf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body><ns2:request>...
body which I genereate with my app I am receiving 我正在接收我的应用程序生成的正文
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:response>...
And that is good. 这很好。 And response in postman is pure XML which I want. 邮递员的反应是我想要的纯XML。
But when I put this endpoint to chrome for example I am receiving website where I can find other endpoints/wsdls to this service but in different language(French, German etc). 但是当我把这个端点放到chrome时,例如我正在接收网站,在那里我可以找到这个服务的其他端点/ wsdls,但是使用不同的语言(法语,德语等)。
EDIT 3 SOLVED 编辑3已解决
My problem was that I have specified Accept-Encoding 我的问题是我已经指定了Accept-Encoding
connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
when I removed it I could read proper response from stream. 当我删除它时,我可以从流中读取正确的响应。
But also I was able to read this message by using GZIPInputStream, but not sure which solution is better. 但我也能通过使用GZIPInputStream来阅读此消息,但不确定哪种解决方案更好。
Maybe problem is in this line new FileOutputStream("C:\\\\Temp\\\\response.html"));
也许问题在于这一行new FileOutputStream("C:\\\\Temp\\\\response.html"));
?and you should change extension to response.xml ?并且您应该将扩展名更改为response.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.