java - 获取请求适用于 POSTMAN,但在我的 Java 应用程序中引发垃圾响应
问题描述
Java 中的新功能,我正在尝试从以下 URL 获取数据; https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY
我可以在没有任何请求标头的情况下使用 POSTMAN 获取,而当我尝试通过 HttpsURLConnection 时,它会抛出
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
代码 :
private void testIt() {
String https_url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY";
URL url;
try {
url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input;
while ((input = br.readLine()) != null) {
System.out.println(input);
}
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
添加大量标题后;
con.setRequestProperty("method", "GET");
con.setRequestProperty("authority", "www.nseindia.com");
con.setRequestProperty("scheme", "https");
con.setRequestProperty("path", "/api/option-chain-indices?symbol=NIFTY");
con.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36");
con.setRequestProperty("sec-fetch-dest", "empty");
con.setRequestProperty("accept", "application/json");
con.setRequestProperty("sec-fetch-site", "same-origin");
con.setRequestProperty("sec-fetch-mode", "cors");
con.setRequestProperty("referer","https://www.nseindia.com/get-quotes/derivatives?symbol=NIFTY&identifier=OPTIDXNIFTY05-03-2020CE11500.00");
con.setRequestProperty("accept-encoding", "gzip, deflate, br");
con.setRequestProperty("accept-language", "en-US,en;q=0.9,hi;q=0.8,es;q=0.7");
con.setRequestProperty("if-none-match", "W/\"dfaed-ueAny3lZCxK1ooMpkoUm3OMoPag");
但是接收垃圾值而不是我通过 POSTMAN 收到的 JSON;
áÑ>^5?T[RãVI÷¤œ;täR^Êj•vW¯`‡1í’'…é½ß߬E=ÝõL#M™„ë¬ê÷AÌÊ›=«¦:*Wáó~O|V•AOq3"/ÃC°T¤B¾2–½ÌW%LЋâ^SÑ^§árG`¦?`$²‚sL›¹ÕÖ)·Ð_J¹¥(?Ûºxœ»ÔÖ)·Üž/s©Íè´]ÀRQûøÊX~¡Km?Ë?™¶?žª¬ñ“
L§—BÑH…C†sÝ•ö}¤;КX«¶xFçûSš\w¥†Ç¹É?Ør»W¹’ýr.ì®[dg?ïâ?èçGQ†^¿úпúž3'go¾í•á¡Œ¼£öÊ,#à jHì…Î$0×ɯ,‡‡|’%²ôe+DN%ó›÷‰UŒæm« xŒ,Ú@ꊀÊÿlçSwý®Oƒpgs—#áu6÷¬²Jgß–¢Î€I–gi3<ÃâÇèöÎκ-ŽÔ,y´àDr*VÖÇ}?Qà'¥Ü™
Þ:%
解决方案
尝试添加请求方法和用户代理。
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int response_code = con.getResponseCode();
if (response_code == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input_line;
StringBuffer response = new StringBuffer();
while ((input_line = in.readLine()) != null) {
response.append(input_line);
}
in.close();
}
}
推荐阅读
- r - Openxlsx dataValidation() 下拉列表中的多项选择
- mysql - 是否有更好/更快的方法来计算日期之间的平均增量?
- flutter - 如何从文档快照中获取特定字段
- r - 不同颜色的评论
- c++ - 将具有三个变量的向量与具有两个变量的向量进行比较
- spring-boot - 我正在部署一个简单的 Spring Boot 应用程序但它显示此错误
- java - 哈希映射没有正确添加元素
- android - 卡片视图阴影效果仅顶部反应原生
- javascript - 如何从 JQuery 代码重写纯 JavaScript 代码?
- reactjs - 当所有数据都已路由到此屏幕时,如何将此移动无字段更新到 Firestore 数据库