由于代理服务器而导致连接超时

时间:2020-06-23 22:08:08

标签: java spring spring-boot rest web-services

我正试图在月食中消耗API。当我在VPN时,它会转到connection timeout。一旦断开vpn的连接,它就会提供正确的输出。谁能指导我如何解决该问题?下面的代码需要什么修改。

package com.shruti.getapi;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class NetClientGet {

    public static void main(String[] args)  {
        
        try
        {
            System.out.println("Inside the main function");
             URL url=new URL("http://dummy.restapiexample.com/api/v1/employees");
             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
             conn.setRequestMethod("GET");
             conn.setRequestProperty("Accept", "application/json");
             if(conn.getResponseCode()!=200)   //Here it goes for connection timeout in VPN
             {
                 System.out.println(conn.getResponseCode());
                 throw new RuntimeException("Failed : HTTP Error Code: "+conn.getResponseCode());
             }
             InputStreamReader in=new InputStreamReader(conn.getInputStream());
             BufferedReader br =new BufferedReader(in);
             String output;
             while((output=br.readLine())!=null)
             {
                 System.out.println(output);
             }
             conn.disconnect();
             
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        
    }
}

1 个答案:

答案 0 :(得分:0)

尝试增加connection的超时时间。

conn.setConnectTimeout(0); // infinite timeout
conn.setReadTimeout(0);    // infinite timeout