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



我正在尝试在日食中消耗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());
}

}
}

尝试增加连接的超时时间。

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

相关内容

  • 没有找到相关文章

最新更新