问题描述
如何使用Java从api网址解析JSON响应?当我运行以下代码我正在收到SocketTimeoutException.但是在Chrome和Microsoft Edge中浏览URL时,我正在获取JSON数据.
how to parse the JSON response from an api url using java? When I run the below codeI 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"(仅用于测试目的.不可用于商业用途)
url:"https://www.nseindia.com/api/quote-derivative?symbol=RELIANCE"(use only for testing purpose. not for commercial use)
Java代码:
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"));
}
}
推荐答案
看看当您从shell curl调用提供的URL时会发生什么:
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://www.nseindia.com/api/quote-derivative?" on this server.<P>
Reference #18.15b5655f.1595338785.264b3809
</BODY>
</HTML>
因此,我想nseindia阻止了该请求,因为它不是来自浏览器.但是,如果您尝试这样做:
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'
您在响应中得到一个json.
you get a json in the response.
{"info":{"symbol":"RELIANCE","companyName":"Reliance Industries Limited",.......}
因此,您需要在请求中添加几个HTTP标头,例如:
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");
这篇关于如何使用Java中的https请求从API URL解析JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!