Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caused by: java.io.IOException: HTTP/1.1 header parser received no bytes

I am getting for the first time

Caused by: java.io.IOException: HTTP/1.1 header parser received no bytes

(my app seemed to work up to now...)

HttpClient httpClient = HttpClient.newHttpClient();
            HttpRequest httpRequest = HttpRequest
                    .newBuilder()
                    .GET()
                    .uri(URI.create("..."))
                    .header("Content-Type", "application/json")
                    .build();

            System.out.print(httpRequest.toString()); // dbg - it's ok

            HttpResponse<String> response = null;
            try {
                response = httpClient.send(httpRequest,
                        HttpResponse.BodyHandlers.ofString());
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }

            if (response != null && response.statusCode() == 200) {
                JSONObject jsonObject = new JSONObject(response.body());
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                ObjectMapper objectMapper = ObjectMapperCreator.getNewObjectMapper();
                try {
                    if (jsonArray.length() > 0) {
                        correctlyCaught = true;
                        return objectMapper.readValue(jsonArray.get(0).toString(), GeocodingResponse.class);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

this is the code. Why am I getting this error?

like image 511
Ob-la-di Avatar asked Oct 14 '20 12:10

Ob-la-di


1 Answers

The HTTP header is the very first thing that the server will respond with.

And that error says that it is empty. In effect then, this error, loosely translated, means: "The server sent absolutely nothing back and hung up".

This also explains why 'it worked before'. It's not you; it's that server. It's broken, offline, or has been updated or replaced so that it can no longer deal with your request.

This + other linked SO answers suggest you force HTTP/1.1 mode - you're just massaging how you send your request in the hope that you can shape it so that the server doesn't choke on it. Most likely the server is just offline and all you have to do is wait a while or get in contact with the administrator. Also try connecting to the URL with e.g. curl or your browser to see what's going on.

Another common reason for this is that they just took their HTTP (as in, the non-HTTPS side) offline or it died and nobody noticed because nobody* connects with HTTP anymore. Then the fix is probably to just.. toss an 's' in that URL. Browsers silently upgrade http:// urls into https:// urls via various mechanisms all the time, but java HTTP libraries don't do that unless you explicitly ask for it (by making that url https based). So, check that URL (it's not in the paste): If it's http://, consider trying again with https:// instead.

Just a sidenote:

} catch (IOException | InterruptedException e) {
               e.printStackTrace();
           }
           if (response != null && response.statusCode() == 200) {

Please, don't do this. What you've written here is: If an error occurs, log it inappropriately (syserr is not an appropriate place to log things) and then silently do nothing. You're setting yourself up for wild goose chases and failure down the road. Do yourself a favour. Go into your IDE, right now, find the place where you configure autocompletion templates. And get rid of that useless, cringe inducing e.printStackTrace() and make that throw new RuntimeException("Uncaught", e);. When things occurs that you did not bother to handle, the last thing you'd want is 'silently do nothing'. You WANT the crash with the appropriate stack trace.

like image 163
rzwitserloot Avatar answered Sep 30 '22 06:09

rzwitserloot