Search code examples
javajsonfiletexthttpurlconnection

How to parse JSON response from an API url using https request in java?


how to parse the JSON response from an api url using java? When I run the below code I am getting SocketTimeoutException. but when browse the URL in the Chrome and Microsoft Edge i am getting the JSON data.

url:"https://www.nseindia.com/api/quote-derivative?symbol=RELIANCE" (use only for testing purpose. not for commercial use)

Java Code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;



public class HTTP_Request {

    public static void main(String[] args) {
        try {
        Send_HTTP_Request.call_me();
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
    
    public static void call_me() throws Exception {
    
        String url ="https://www.nseindia.com/api/quote-derivative?symbol=RELIANCE";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // optional default is GET
        con.setRequestMethod("GET");
        //add request header
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        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;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        
        //print in String
        System.out.println(response.toString());
        
        //Read JSON response and print
       // JSONObject myResponse = new JSONObject(response.toString());
       // System.out.println("result after Reading JSON Response");
       // System.out.println("origin- "+myResponse.getString("origin"));
         
        }
}

Solution

  • look what happens when you call the provided url from a shell curl:

    > $ curl 'https://www.nseindia.com/api/quote-derivative?symbol=RELIANCE'                                                 [±master ●●▴]
    <HTML><HEAD>
    <TITLE>Access Denied</TITLE>
    </HEAD><BODY>
    <H1>Access Denied</H1>
    
    You don't have permission to access "http&#58;&#47;&#47;www&#46;nseindia&#46;com&#47;api&#47;quote&#45;derivative&#63;" on this server.<P>
    Reference&#32;&#35;18&#46;15b5655f&#46;1595338785&#46;264b3809
    </BODY>
    </HTML>
    

    So, I guess nseindia is blocking the request because is not coming from a browser. But, if you try this:

    curl 'https://www.nseindia.com/api/quote-derivative?symbol=RELIANCE' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Connection: keep-alive' -H 'Cookie: ak_bmsc=D709927FAB6A44F1C243E20E9199ABA35F65B5158B040000E1EF165F44B95756~plIifDJa9J1EHznQiR/zQpjmGEFWOE88N83Yuqa0oOcgvvYh7eLlgD5MSFVhCZZOObVMZ7UDwEzmOQ2V2lJ6W9pPf6zEbQT1Je27i6h3hrAIyBYFMplV1EDo7rSLxXmCk+HGL3ynHmuPJsePDPD7WTljjTMnM1qpvixsCMEtM1BlmVmuXQijd8cKjWxLeLaf/cNAEJB6VC7SXOq+j6uj6oi9xF/Z2NYB905XnUg9YppXM=' -H 'Upgrade-Insecure-Requests: 1' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache'
    

    you get a json in the response.

    {"info":{"symbol":"RELIANCE","companyName":"Reliance Industries Limited",.......}
    

    So you need to add a couple of HTTP Headers to your request, something like:

    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    con.setRequestProperty("Accept", "text/html");