【问题标题】:XML response from URL not correct in Java, but correct in browser来自 URL 的 XML 响应在 Java 中不正确,但在浏览器中正确
【发布时间】:2012-08-20 16:30:25
【问题描述】:

首先我想说的是,我能够在 Stackoverflow 的帮助下从 URL 读取 XML 响应。感谢您的帮助和许多其他帮助。我阅读并打印了响应(下面的代码),但结果不正确。

在浏览器中显示(正确):

<ENELIT>
<GESI>
<RI>
<NR id="008201dfa306f4a2" tu="N" nr="0412395504" ut="" cp="" dp="" tm="" ca="" da="" rt="" cc="4"/>
</RI>
</GESI>
</ENELIT>

在 Java 中它会打印这个(不正确):

<html>
  <head>
    <link rel=stylesheet href="/psiche/styles/IEStyle.css" type="text/css">
  </head>
  <body>

    <SCRIPT FOR=window EVENT=onload LANGUAGE="JAVAScript">
      if (top.areaApplication != undefined)
      {
        alert("Sessione utente scaduta. Effettuare il logon");
        top.areaApplication.location = "/gesi/online/root/login.htm";
      } 
    </SCRIPT>
    <h2>Sessione scaduta, Effettuare il logon.</h2> 
  </body>   
</html>

我怎样才能得到正确的结果?谢谢!

我获取和打印响应的代码: 示例 1:

URL url = new URL("http://gesi-ro-test.banat.enelro:8010/dynamic/gesi/ri/elab/phonerequest/wind.serid=008201dfa306f4a2&nr=06648624&is=2011/04/20%2013:03:03.296&rt=RE/");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));

String inputLine;
while ((inputLine = in.readLine()) != null)
      System.out.println(inputLine);
in.close();

示例 2:

URL url = new URL("http://gesi-ro-test.banat.enelro:8010/dynamic/gesi/ri/elab/phonerequest/wind.ser?id=008201dfa306f4a2&nr=06648624&is=2011/04/20%2013:03:03.296&rt=RE");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream stream = connection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
    sb.append(line).append("\n");
}
stream.close();
System.out.println(sb.toString());

【问题讨论】:

    标签: java url servlets xmlhttprequest


    【解决方案1】:

    如果不说特定语言,您似乎需要先登录服务才能访问您的信息。

    【讨论】:

    • 但是为什么在浏览器中它无需登录就返回响应?
    • @mamba 因为您的浏览器可能正在存储身份验证 cookie
    • 好的,当我用 Java 程序编写它时,它就像一个魅力,但在 servlet 中它仍然返回错误。为什么会这样?
    • 我的网址好像不行。我会将此答案标记为确定。谢谢!
    猜你喜欢
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2016-07-29
    • 1970-01-01
    相关资源
    最近更新 更多