解决 url.openStream() 超时问题 time out

一开始问题是

 

  //1.获取url的输入流 dataInputStream
            DataInputStream dataInputStream=new DataInputStream(url.openStream());
                    
 //2.加一层BufferedInputStream
            BufferedInputStream bufferedInputStream=new BufferedInputStream(dataInputStream);
        

后来发现  是因为没用代理 

 

改成           

// 本地不能配置这个代理,只有华为云服务器上的项目访问外网时,会需要配置这个代理
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("—————", 8888));
            HttpURLConnection htpcon = (HttpURLConnection) url.openConnection(proxy);       
            
               
             //HttpURLConnection htpcon = (HttpURLConnection) url.openConnection();
            htpcon.setRequestMethod("GET");
            htpcon.setDoOutput(true);
            htpcon.setDoInput(true);
            htpcon.setUseCaches(false);
            htpcon.setConnectTimeout(1000);
            htpcon.setReadTimeout(1000);
            InputStream in = htpcon.getInputStream();
            

              BufferedInputStream bufferedInputStream=new BufferedInputStream(in);
 

-----===========================================================================================

==============================================================================================

参考了  https://www.cnblogs.com/jinhaidong/p/6893242.html  

 

随笔 - 15  文章 - 0  评论 - 0

url().openStream 的超时问题处理

1 InputStream is = new URL("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip").openStream(); 
2 BufferedReader rd = new BufferedReader(InputStreamReader(is, Charset.forName("UTF-8"))); 

 上图可能出现 超时的情况。

复制代码

 1 URL url = new URL("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip);
 2 HttpURLConnection htpcon = (HttpURLConnection) url.openConnection();
 3 htpcon.setRequestMethod("GET");
 4 htpcon.setDoOutput(true);
 5 htpcon.setDoInput(true);
 6 htpcon.setUseCaches(false);
 7 htpcon.setConnectTimeout(1000);
 8 htpcon.setReadTimeout(1000);
 9 InputStream in = htpcon.getInputStream();
10 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));

 

 

 

 

你可能感兴趣的:(Java)