0

I can read full response body (big JSON data, more than 400,000 chars) few times, but after 5-6 times my response is not full.

Here's my code of getting response:

        URL address = new URL("https://myurl.com/");
        HttpURLConnection Connection = (HttpURLConnection)address.openConnection();

        Connection.setRequestMethod("GET");
        Connection.setRequestProperty("accept-language", "en-US");
        Connection.setRequestProperty("user-agent", UserAgent);

        Connection.setRequestProperty("cookie", cookies);

        Connection.setUseCaches(false);
        Connection.setDoInput(true);
        Connection.setDoOutput(true);

        if (Connection.getResponseCode() == HttpsURLConnection.HTTP_OK)
        { 
            String Response = new String();
            InputStream is = Connection.getInputStream();
            int ch;
            StringBuffer sb = new StringBuffer();
            while (( ch = is.read()) != -1) {
                sb.append((char) ch);
            }
            Response = sb.toString();
            is.close();
        }

In my original code after is.close(), it is just lot of JSON parsing from Response string

  • 1
    Why do you read the response body byte by byte? –  Dec 05 '16 at 13:50
  • I'm beginner and I found that way to read response and it worked, so I used that. Can you post some better ways? Thanks – WiktorCode Dec 05 '16 at 13:52
  • Hint: I Java variable names don't start with a capital letter. Use `connection` and `response` as variable names. –  Dec 05 '16 at 13:53
  • OK, but I guess it won't resolve my problem – WiktorCode Dec 05 '16 at 13:55
  • Try any of the `copy` methods of [`IOUtils`](https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/IOUtils.html) to copy the `InputStream` to a `ByteArrayOutputStream`. –  Dec 05 '16 at 13:57
  • What do you mean by "my response is not full?" – xehpuk Dec 05 '16 at 13:59
  • Full response is json data, so it looks like: { "name":"value" } Not full like that: { "name":"va - It just is not full response body. Original body is soo long, so few thousands of characters are "out" – WiktorCode Dec 05 '16 at 14:00
  • 1
    Your code doesn't seem to be your real code because `Response` is scoped to the `if`. Posting an [MCVE](http://stackoverflow.com/help/mcve) may help. – xehpuk Dec 05 '16 at 14:27
  • @xehpuk It's not my real code, but it presents how my code works, so have you any ideas how to fix my problem? – WiktorCode Dec 05 '16 at 14:31
  • Maybe you are hitting a limit on the size of the request. Here's an answer about that: http://stackoverflow.com/questions/2364840/what-is-the-size-limit-of-a-post-request – gregbert Dec 05 '16 at 14:35
  • @gregbert I don't think so. Why can I grab first 5 responses, but 6th would be cutted? First response is much bigger than next ones – WiktorCode Dec 05 '16 at 14:41
  • No, not with the code you provided. – xehpuk Dec 05 '16 at 14:46
  • @xehpuk My original code has just parsing from Response string after is.close(), but I think it's useless information. – WiktorCode Dec 05 '16 at 14:51
  • How about your `Connection` variable? Are you reinitializing that every time you hit the endpoint? What, if anything, do you do if it's not an 200 response? Is the partial response what you are expecting up to the point it is cut off? – gregbert Dec 05 '16 at 15:04
  • @gregbert Yes, I'm reinitializing that variable everytime I send request. My function supports just 200 response. Sorry, but I don't understand your 3rd question. – WiktorCode Dec 05 '16 at 15:16
  • Can you write up a [MCVE] of this, including a url to your actual data? There is no way anyone can help you with this by making guesses about something that isn't even the code you're running and they can't try themselves. – pvg Dec 05 '16 at 15:18
  • Did you checked the response of this query on a browser, to check if yuo get everything ? There is a limit in the response length like _gregbert_ said – AxelH Dec 05 '16 at 15:20
  • As a debugging option, try hitting the endpoint with postman ( https://www.getpostman.com/ ) and see if you get the same response after 5 calls. Or just in your browser since it's a GET – gregbert Dec 05 '16 at 15:20

0 Answers0