0

I'm building a backend server side java application that uses a post method to send data to an API, based on an example I've been given in Python and C# (console applications). This API has specific responses associated with various response codes. I tried using the code below, but the response message just seems to be the generic system message associated with the response code, not the message on the API I'm accessing.

 System.out.println("Response Code : " + connection.getResponseCode());
 System.out.println("Response Message : " + connection.getResponseMessage());

I saw in the C# example that they were using WebException error handling, but I can't find the Java equivalent, if there even is one.

Edit: This is the error I'm getting with a 403, in the python example, it returned a custom response

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://[redacted]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1926)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1921)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1920)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:59)
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: https://api.rentrak.com/tv/v3/national_airing_views
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:56)

This is the code I'm using to build the response

    StringBuilder response;
    try (BufferedReader in = new BufferedReader(
            new InputStreamReader(connection.getInputStream()))) {
        String inputLine;
        response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
    }
4
  • If I understand very you want to get response code of an http response? wright? Commented Dec 16, 2016 at 14:59
  • I want to get the response message that's stored on the API Commented Dec 16, 2016 at 15:03
  • If the invoked WS is a SOAP one you'll get a SOAPFaultException for unmodeled faults and an ordinary subclass of exception for the modeled ones. See this Commented Dec 16, 2016 at 15:05
  • you can add enclose the code using try catch. then you will get any exception associated with your post request. in code code your are getting http response code and http response message(like code:200, message: ok). if want to get response body of you request you have to call getResponsebody() Commented Dec 16, 2016 at 15:08

1 Answer 1

1

If you use just java.net.HttpURLConnection

for success response there is connection.getInputStream() to get response body.

But for error response there is connection.getErrorStream() where error response body will be.

I mean as example server returns HTTP 404 - Not found error with custom HTML page. That HTML will be in Error Stream not in Input Stream.

Of course response code is in connection.getResponseCode() as int (i.e. 404) and response message is in getResponseMessage() (i.e. "Not Found")

Sign up to request clarification or add additional context in comments.

8 Comments

Response Error : sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@36d4b5c Is the error I got, I am using HttpsURLConnection, I got the authorization key to work, just can't get back a successful response.
HttpURLConnection is abstract and HttpsURLConnection is extention of it and is also abstract. sun.net.www.protocol.http.HttpURLConnection is an implementation. could you post piece of your code and at what point you got that Response Error?
As of now it looks like when you get that - you have to read actual response from that stream. Because sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@‌​36d4b5c is a result of default toString() message from object (i.e. HttpInputStream) when toString() is not implemented.
I guess you are almost there... just read what is in that Stream, then print it.
I am using getInputStream, but I'm coming back with this messy error (edited above). If the response is a JSON object, is Java just capable of printing out the text or do I specifically need something to decode the JSON text? I'm asking because I was under the impression that JSON was really just a structuring mechanism for text more so than an actual object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.