0

I'm using Processing.

I have this query for a server:

// temp is a String containing this looong URL

try{
    xml = loadXML(temp);
    }catch(Exception e){
      e.printStackTrace();
    }

I intentionally made a mistake in the URL. When I copy and paste the URL in browser (or click it above) the browser displays a nice message specifying the error made. "Missing parameter: autor". I'd like to access this information to display it to user. But the stackTrace is much more cryptographic (pasted at the end) and do not contains the useful message above... How could I get that message? If i do loadStrings("http://stackoverflow.com") for instance, I got all HTML as strings, that would be good enough for me, but with the error 500 in the url above I got an error and don't reach the HTML, but the browser does... How? Does the server serves some alternate html? Is there this address? Any more info needed?

Processing 2.0 willing to be javaScript compatible.

thanks

this is the html in the error page:

    <html>
      <head></head>
      <body>
      <pre style="word-wrap: break-word; white-space: pre-wrap;">Missing parameter:                   autor. </pre>                
      </body>
    </html>

"Java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.camara.gov.br/SitCamaraWS/Proposicoes.asmx/ListarProposicoes?sigla=PL&numero=&ano=1960&datApresentacaoIni=&datApresentacaoFim=&parteNomeAutor=&siglaPartidoAutor=&siglaUFAutor=&generoAutor=&IdSituacaoProposicao=&IdOrgaoSituacaoProposicao=&&codEstado=&codOrgaoEstado=&emTramitacao= at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436) at java.net.URL.openStream(URL.java:1010) at processing.core.PApplet.createInputRaw(PApplet.java:6960) at processing.core.PApplet.createInput(PApplet.java:6928) at processing.core.PApplet.createReader(PApplet.java:6722) at processing.core.PApplet.loadXML(PApplet.java:6070) at processing.core.PApplet.loadXML(PApplet.java:6060) at buildingQuery2_class$Query.makeQuery(buildingQuery2_class.java:161) at buildingQuery2_class.setup(buildingQuery2_class.java:23) at processing.core.PApplet.handleDraw(PApplet.java:2245) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243) at processing.core.PApplet.run(PApplet.java:2141) at java.lang.Thread.run(Thread.java:680) http://www.camara.gov.br/SitCamaraWS/Proposicoes.asmx/ListarProposicoes?sigla=PL&numero=&ano=1960&datApresentacaoIni=&datApresentacaoFim=&parteNomeAutor=&siglaPartidoAutor=&siglaUFAutor=&generoAutor=&IdSituacaoProposicao=&IdOrgaoSituacaoProposicao=&&codEstado=&codOrgaoEstado=&emTramitacao= does not exist or could not be read java.net.MalformedURLException at java.net.URL.(URL.java:601) at java.net.URL.(URL.java:464) at java.net.URL.(URL.java:413) at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:649) at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284) at processing.data.XML.(XML.java:170) at processing.core.PApplet.loadXML(PApplet.java:6070) at processing.core.PApplet.loadXML(PApplet.java:6060) at buildingQuery2_class$Query.makeQuery(buildingQuery2_class.java:161) at buildingQuery2_class.setup(buildingQuery2_class.java:23) at processing.core.PApplet.handleDraw(PApplet.java:2245) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243) at processing.core.PApplet.run(PApplet.java:2141) at java.lang.Thread.run(Thread.java:680)

v.k.
  • 2,826
  • 2
  • 20
  • 29

1 Answers1

0

I got a good answer for that from processing forum by PhiLho.. I'll leave it here for possible future readers:

//code from PhiLho at:
//http://forum.processing.org/topic/how-can-i-get-html-error-description-from-server-when-loading-xml

import java.net.*;
import java.io.*;
String url1 = "http://www.camara.gov.br/SitCamaraWS/Proposicoes.asmx/ListarProposicoes?sigla=PL&numero=&ano=1960&datApresentacaoIni=&datApresentacaoFim=&parteNomeAutor=&siglaPartidoAutor=&siglaUFAutor=&generoAutor=&IdSituacaoProposicao=&IdOrgaoSituacaoProposicao=&&codEstado=&codOrgaoEstado=&emTramitacao=";
String url2 = "http://Processing.org";
// With information frm http://stackoverflow.com/questions/4596447/java-check-if-file-exists-on-remote-server-using-its-url
URL url = null;
try
{
  url = new URL(url1);
}
catch (MalformedURLException e)
{
  println("Error in URL " + e);
  exit();
}

HttpURLConnection connection = null;
try
{
  connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod("GET");
  int code = connection.getResponseCode();
  if (code != HttpURLConnection.HTTP_OK)
  {
    println("Answer: " + code + " - " + connection.getResponseMessage());
    InputStream error = connection.getErrorStream();
    if (error != null)
    {
      println("Error response:");
      String[] lines = loadStrings(error);
      println(lines);
      error.close();
    }
    println("Error header:");
    String field = null;
    int i = 0;
    do
    {
      field = connection.getHeaderField(i);
      String key = connection.getHeaderFieldKey(i);
      if (field != null)
      {
        println("Header " + i + ": " + (key == null ? "" : key + "=") + field);
      }
      i++;
    } while (field != null);
    println("End in error");
    exit();
  }
}
catch (IOException e)
{
  println("Error in I/O " + e);
  exit();
}
println("Correct data:");
InputStream input = null;
try
{
  connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod("GET");
  input = connection.getInputStream();
  String[] lines = loadStrings(input);
  println(lines);
}
catch (IOException e)
{
  println("Error " + e);
}
finally
{
  if (input != null)
  {
    try { input.close(); } catch (IOException e) {}
  }
}
exit();
v.k.
  • 2,826
  • 2
  • 20
  • 29