java SpringBoot应用程序:阅读URL进入等待状态并卡住了我的项目

ibrsph3r  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(39)

我在项目中使用以下API

GET:请求

https://myhost.com/api/option-chain-indices?symbol=ALL

这个URL在浏览器中工作,也在像PostMen这样的RESTClient应用程序中工作,但是当我调用我的项目时,它将进入等待状态
我正在使用许多API从不同的服务器的工作。但从这个服务器myhost.com等待响应。。我已经等待响应超过30分钟
我已经尝试了很多事情来获取数据的网址,但都是失败的
例一:

public void myFunction() {

        String output = "";
        Root data = null;

        try {

            URL url1 = new URL("https://myhost.com/api/option-chain-indices?symbol=ALL");
            HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
            connection1.setRequestMethod("GET");
            connection1.setRequestProperty("Accept", "application/json");
 
            if (connection1.getResponseCode() != 200) {
                System.out.println("NSE INIT : Failed : HTTP Error code : " + connection1.getResponseCode());
            } else {

                InputStreamReader in = new InputStreamReader(connection1.getInputStream());
                BufferedReader br = new BufferedReader(in);
                while ((output = br.readLine()) != null) {
                    data = objectMapper.readValue(output, Root.class);
                }
            }
            connection1.disconnect();

        } catch (Exception e) {
            System.out.println("Exception" + e);
        }
    }

字符串
示例2:我也使用了WebClient

@Service
public class MyService {

    private final WebClient webClient;

    public MyService(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.build();
    }

    public void fetchDataFromAPI() {

        System.out.println("Get Data ....");
        this.webClient.get()
                .uri("https://myhost.com/api/option-chain-indices?symbol=ALL")
                .retrieve()
                .bodyToMono(String.class)
                .subscribe(response -> {
                    // Handle the response here
                    System.out.println("Received response: " + response);
                });

    }
}

h79rfbju

h79rfbju1#

根据@Haridarshan的评论,这解决了问题:

connection1.setRequestProperty("User-Agent","nSense/1.0 (nSense Technologies)");

字符串

相关问题