How to get correct data from HTTP Request

HCarrasko

I'm trying to get my user information from stackoverflow api using a simple HTTP request with GET method in Java.

This code I had used before to get another HTTP data using GET method without problems:

    URL obj;
    StringBuffer response = new StringBuffer();
    String url = "http://api.stackexchange.com/2.2/users?inname=HCarrasko&site=stackoverflow";
        try {
        obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        in.close();
        System.out.println(response.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

But in this case I'm getting just stranger symbols when I print the response var, like this:

�mRM��0�+�N!���FZq�\�pD�z�:V���JX���M��̛yO^���뾽�g�5J&� �9�YW�%c`do���Y'��nKC38<A�&It�3��6a�,�,]���`/{�D����>6�Ɠ��{��7tF ��E��/����K���#_&�yI�a�v��uw}/�g�5����TkBTķ���U݊c���Q�y$���$�=ۈ��ñ���8f�<*�Amw�W�ـŻ��X$�>'*QN�?�<v�ݠ FH*��Ҏ5����ؔA�z��R��vK���"���@�1��ƭ5��0��R���z�ϗ/�������^?r��&�f��-�OO7���������Gy�B���Rxu�#:0�xͺ}�\�����

thanks in advance.

Spencer D

The content is likely GZIP encoded/compressed. The following is a general snippet that I use in all of my Java-based client applications that utilize HTTP, which is intended to deal with this exact problem:

// Read in the response
// Set up an initial input stream:
InputStream inputStream = fetchAddr.getInputStream(); // fetchAddr is the HttpURLConnection

// Check if inputStream is GZipped
if("gzip".equalsIgnoreCase(fetchAddr.getContentEncoding())){
    // Format is GZIP
    // Replace inputSteam with a GZIP wrapped stream
    inputStream = new GZIPInputStream(inputStream);
}else if("deflate".equalsIgnoreCase(fetchAddr.getContentEncoding())){
    inputStream = new InflaterInputStream(inputStream, new Inflater(true));
} // Else, we assume it to just be plain text

BufferedReader sr = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
StringBuilder response = new StringBuilder();
// ... and from here forward just read the response...

This relies on the following imports: java.util.zip.GZIPInputStream; java.util.zip.Inflater; and java.util.zip.InflaterInputStream.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the data from PUT HTTP Request (form-data)

From Dev

How to get the data from PUT HTTP Request (form-data)

From Dev

Selenium Python get data from HTTP request

From Dev

elixir plug: correct way to get form data from a post request

From Dev

How to make an HTTP request from raw data?

From Dev

How do I get data to show up in angular ui.grid from an $http request

From Java

How to get data out of a Node.js http get request

From Dev

How to get the data response from ajax request?

From Dev

How to get the data response from ajax request?

From Dev

How to get data from request Nodejs

From Dev

How to get data from POST request

From Dev

binding data from $http.get() request into ng-repeat

From Dev

Using jquery in angular in returning data from $http.get request

From Dev

Using jquery in angular in returning data from $http.get request

From Dev

How to get only Message Body from a GET HTTP request?

From Dev

How does JSON data get sent in an http request?

From Dev

Get http request data in Django Url Dispatcher? Is this possible? If so, how?

From Dev

How to include structured data as a parameter of a HTTP GET request?

From Dev

How get clear http-request data in component?

From Dev

How do I get data out of a Node http(s) request?

From Dev

JQuery GET request won't get correct data from php echo

From Dev

HTTP Get Request from NodeJS

From Dev

HTTP Get Request from NodeJS

From Dev

How to get host name with port from a http or https request

From Dev

How to get the status code from within the HTTP request handler

From Dev

How to get the JSESSIONID from a HTTP request using ContainerRequestContext?

From Dev

How to make $http.get request from within directive controller?

From Dev

Akka-http: How to get custom header from a request?

From Dev

How to get HTTP status code from WinHttp request?

Related Related

  1. 1

    How to get the data from PUT HTTP Request (form-data)

  2. 2

    How to get the data from PUT HTTP Request (form-data)

  3. 3

    Selenium Python get data from HTTP request

  4. 4

    elixir plug: correct way to get form data from a post request

  5. 5

    How to make an HTTP request from raw data?

  6. 6

    How do I get data to show up in angular ui.grid from an $http request

  7. 7

    How to get data out of a Node.js http get request

  8. 8

    How to get the data response from ajax request?

  9. 9

    How to get the data response from ajax request?

  10. 10

    How to get data from request Nodejs

  11. 11

    How to get data from POST request

  12. 12

    binding data from $http.get() request into ng-repeat

  13. 13

    Using jquery in angular in returning data from $http.get request

  14. 14

    Using jquery in angular in returning data from $http.get request

  15. 15

    How to get only Message Body from a GET HTTP request?

  16. 16

    How does JSON data get sent in an http request?

  17. 17

    Get http request data in Django Url Dispatcher? Is this possible? If so, how?

  18. 18

    How to include structured data as a parameter of a HTTP GET request?

  19. 19

    How get clear http-request data in component?

  20. 20

    How do I get data out of a Node http(s) request?

  21. 21

    JQuery GET request won't get correct data from php echo

  22. 22

    HTTP Get Request from NodeJS

  23. 23

    HTTP Get Request from NodeJS

  24. 24

    How to get host name with port from a http or https request

  25. 25

    How to get the status code from within the HTTP request handler

  26. 26

    How to get the JSESSIONID from a HTTP request using ContainerRequestContext?

  27. 27

    How to make $http.get request from within directive controller?

  28. 28

    Akka-http: How to get custom header from a request?

  29. 29

    How to get HTTP status code from WinHttp request?

HotTag

Archive