100+并发报错 java.net.BindException: Address already in use

℡╲_俬逩灬. 提交于 2020-04-09 17:02:44
直接上代码,注意红色部分就是解决问题的方法,很多人都会忽视这个细节
public class HttpClient {
    public static String doGet(String httpurl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;// 返回结果字符串
        try {
            
            URL url = new URL(httpurl);
           
            connection = (HttpURLConnection) url.openConnection();
           
            connection.setRequestMethod("GET");
            // close_wait状态的连接,而且这些连接都不会关闭,最后导致服务器没法建立新的网络连接,从而停止响应
            connection.setRequestProperty("Connection", "close");
           
            connection.setConnectTimeout(60000);
           
            connection.setReadTimeout(60000);
          
            connection.connect();
          
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
               
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }else {
                System.out.println("------   "+connection.getResponseCode());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch(Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
            connection.disconnect();// 关闭远程连接
        }
 
        return result;
    }
 
    public static String doPost(String httpUrl, String param) {
 
        HttpURLConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        BufferedReader br = null;
        String result = null;
        try {
            URL url = new URL(httpUrl);
          
            connection = (HttpURLConnection) url.openConnection();
         
            connection.setRequestMethod("POST");
            
            connection.setConnectTimeout(15000);
           
            connection.setReadTimeout(60000);
 
           
            connection.setDoOutput(true);
            
            connection.setDoInput(true);
            
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
           
            connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
           
            os = connection.getOutputStream();
          
            os.write(param.getBytes());
          
            if (connection.getResponseCode() == 200) {
 
                is = connection.getInputStream();
               
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
 
                StringBuffer sbf = new StringBuffer();
                String temp = null;
              
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 断开与远程地址url的连接
            connection.disconnect();
        }
        return result;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!