简体   繁体   English

Get 请求适用于 POSTMAN 但在我的 Java 应用程序中引发垃圾响应

[英]Get request works with POSTMAN but throws garbage response in my Java application

New in Java, I am trying to fetch data from the following URL; Java 中的新手,我正在尝试从以下 URL 获取数据; https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY

I can fetch using POSTMAN without any request header while when I am trying through HttpsURLConnection it's throwing as我可以在没有任何请求标头的情况下使用 POSTMAN 获取,而当我尝试通过 HttpsURLConnection 时,它会抛出

java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)

Code :代码 :

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();
        }
    }

After adding lots of headers as;添加大量标题后;

  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");

But receiving garbage values rather than JSON which I have received through POSTMAN as;但是接收垃圾值而不是我通过邮递员收到的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 add requestmethod and user-agent.尝试添加 requestmethod 和用户代理。

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();
    }
}

删除下面的标题后,它对我有用,

con.setRequestProperty("accept-encoding", "gzip, deflate, br");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 中的 403 响应代码,但在 Postman 中有效 - 403 response code in Java, but works in Postman REST请求在RestClient中有效,但在PostMan / Java中无效 - REST request works in RestClient but not PostMan/Java 获取请求在 Postman 中有效,但在 Java 中通过 HttpURLConnection 调用请求时给出 403 - Get Request Works in Postman but gives a 403 When Request is Called through HttpURLConnection in Java GET 请求适用于 Postman,但不适用于 SpringBoot RestTemplate - GET Request Works in Postman but not with SpringBoot RestTemplate RestTemplate response.getBody对put和post请求的4 **和5 **错误抛出异常但对get请求工作正常 - RestTemplate response.getBody throws exception on 4** and 5** errors for put and post request but works fine for get requests 从Ext / Java应用程序中的JSONP请求获取响应文本 - Get response text from a JSONP request in Ext / Java Application POST 请求在 Java 客户端中不起作用,但在 postman 中起作用 - POST request does not work in Java client but works in postman Java HTTP获取请求比邮递员获取请求慢 - Java http get request slower than postman get request Java应用程序中的垃圾回收 - Garbage Collection in Java application 从POSTMAN到我的Java API的POST上的405响应代码 - 405 response code on POST to my java api from POSTMAN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM