HttpURLConnection连接被拒绝的问题

时间:2020-06-10 13:38:36

标签: tomcat httpurlconnection connection-pooling webservice-client connection-refused

我在tomcat中正在获取正在上传文件的webservice客户端的连接被拒绝的问题。几天后,此问题仅在生产服务器中发生。最初它可以正常工作,一周后我收到了这个问题。重新启动服务器可以解决该问题,但是几天后会再次发生。请找到下面的代码

            HttpURLConnection conn = null;
            InputStream inputStream = null;
            OutputStream outputStream =null;
            StringBuilder sb = new StringBuilder(); 

            try {

            URL url = new URL(sFullWSURL);
            conn = (HttpURLConnection) url.openConnection();

            conn.setDoOutput(true);             
            conn.setDoInput(true);
            conn.setUseCaches (false);              
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/octet-stream");

            //Add Basic Auth Header:
            String sAuthString = sWSUsername + ":" + sWSPassword;
            byte[] abtAuthStringBytes = sAuthString.getBytes();
            String sAuthStringBase64 =  
            java.util.Base64.getEncoder().encodeToString(abtAuthStringBytes);
            conn.setRequestProperty("Authorization", "Basic " + sAuthStringBase64);         
            conn.connect();

            if((abtBytes != null) && (abtBytes.length > 0))
            {
                    outputStream = conn.getOutputStream();
                    DataOutputStream output = new DataOutputStream(outputStream);
                    output.write(abtBytes);
                    output.flush();                    
                    output.close();

                    inputStream = conn.getInputStream();                        
                    BufferedReader bufReader = new BufferedReader(new nputStreamReader(inputStream));

                    String str = null;
                    while (null != ((str = bufReader.readLine()))) 
                    {
                        sb.append(str);
                    }
                    _log.info("filenewuploadjob response :"+sb.toString());             
                    bufReader.close();
                }
            }catch(Exception e) {
                _log.error("Error while calling filenewuploadjob :"+e.getMessage(),e);                  
            }
            finally
            {
                if(inputStream != null) {
                    inputStream.close();
                }
                if(outputStream != null) {
                    outputStream.close();
                }
            }
            return sb.toString();

0 个答案:

没有答案