Capturing server exceptions via Java HTTP request

Asked

Viewed 361 times

0

Hello, I have a system that reads XML files, transforms into JSON and makes HTTP (POST) requests to another server to save the data in the database. But some XML files have, for example, null fields that the server cannot save without, and then the server returns the exception. This is my server connection class:

public class Conexao {
public void sendPost(String json, URL url) throws Exception {

    try {

        // Cria um objeto HttpURLConnection:;
        HttpURLConnection request = (HttpURLConnection)  
                url.openConnection();

        try {
            // Define que a conexão pode enviar informações e obtê-las de volta:
            request.setDoOutput(true);
            request.setDoInput(true);

            // Define o content-type:
            request.setRequestProperty("Content-Type", "application/json");

            // Define o método da requisição:
            request.setRequestMethod("POST");

            // Conecta na URL:
            request.connect();

            // Escreve o objeto JSON usando o OutputStream da requisição:
            try (OutputStream outputStream = new BufferedOutputStream(request.getOutputStream())) {
                outputStream.write(json.getBytes("UTF-8"));
                outputStream.flush();
                outputStream.close();

                } 

            int response = request.getResponseCode();
            BufferedReader br;
            if (200 <= response && response <= 299) {
                //Requisição feita com sucesso
            }  else {
               br = new BufferedReader(new InputStreamReader((request.getErrorStream())));
               String resul = br.readLine();
               throw new Exception(" Dados da requisição: " + resul);

                }
           }
        } finally {
            request.disconnect();
          }

    } catch (Exception e) {

        throw (e);

    }

        }

}

Until then ok, but I need to capture these exceptions and show in my system why the file has not saved in the bank. When I run this function and the server error, "resul" returns me to the server HTML, full of unnecessary information and most of the time without the error caused. Can anyone tell me if it is possible to capture only the exception caused through Httpurlconnection Sponse?

  • 2

    If you have control over the server, ideally you would provide an API on the server to the client and, if the client passed invalid information, the server would send a JSON or XML describing the error as a response. If you don’t have control over the server and can only rely on the HTML response, what you could do is use an HTML parser like the JSOUP to try to catch the error inside the HTML. Not knowing what the server application or answer is like, it is difficult to give an objective answer.

  • We have control of the server yes, we use spring boot and Hibernate, knowing this you would indicate another resource for the server!

  • Place the server code responsible for processing the request.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.