getInputStream()的代码与Java 1.6兼容,但是已经有1.7了

时间:2013-03-30 21:03:58

标签: java

我有一个软件可以在PC和机器之间建立通信,以前在Java 1.6中工作,但在1.7中不再存在。 IOException - > “无效的Http响应”是我在调用getInputStream()方法时得到的。看起来这个方法已得到改进,而且更敏感,这意味着responseCode = 在Java 1.6中没有专门检查-1结果。

Assoth with Wireshark我检查了两个版本(1.6和1.7)是否发送和接收了相同的以太网帧,两者都是如此。

我只能从PC(客户端)端解决此问题...这意味着无法编辑或更改服务器代码。

我会在如何修改或实现新的东西以使代码兼容ver方面提供任何帮助。 1.7因为我不是以前的程序员...谢谢

序列:

  1. get被称为
  2. 返回readResponse(..)被称为
  3. getInputStream() - > IOException的
  4. catch(Exception e){System.out.println(“error sending get request”+ getURL()+“string:”+ requestString); return Error.ethernetException; // TODO
  5. 此处为控制台输出:

    -1 错误发送获取请求...字符串:* APPLETCOMMINFO_ strResponse:以太网异常

    和代码:

    private String get(String requestString)/* throws ComunicationException*/ {
        HttpURLConnection httpURLConnection = null;
        OutputStream out = null;        
    
        try {   
            String encodedRequestString = URLEncoder.encode(requestString, "iso-8859-1");
            String path = getURL().getPath();
            if (!path.endsWith("/")) path = path + "/";
            path = path + encodedRequestString;
            URL fullURL = new URL(getURL().getProtocol(), getURL().getHost(), getURL().getPort(), path);
            httpURLConnection = (HttpURLConnection)fullURL.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setDoOutput(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setUseCaches(false);
    
            // Set timeout at 5s
            httpURLConnection.setConnectTimeout(m_nTimeOut);
            httpURLConnection.setReadTimeout(m_nTimeOut);
    
            return readResponse(httpURLConnection);
    
        } catch (Exception e) {
            System.out.println("error sending get request " + getURL() + " string: " + requestString);
            return Error.ethernetException; //TODO
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Throwable t) {
                    System.out.println("GET: out.close(), Class: Client");
                }
            }
    
            if (httpURLConnection != null) {
                try {
                    httpURLConnection.disconnect();
                } catch (Throwable t) {
                    System.out.println("GET: httpURLConnection.disconnect(), Class: Client");
                }
            }
        }       
    }
    
    /**
     * Reads the response from the server into a string.
     * The content length header must be set!
     * @param connection
     * @return
     * @throws IOException
     */
    private String readResponse(HttpURLConnection connection) throws IOException {
        if (connection == null) throw new IllegalStateException("connection must not be null");
    
         connection.connect();
    
        int status = connection.getResponseCode();
        System.out.println(status);
    
        // InputStream aaa = connection.getInputStream();
    
    
        Reader reader = null;
        try {           
            reader = new InputStreamReader(connection.getInputStream(), "iso-8859-1");
    
            int readBufferSize = connection.getContentLength();
            if (readBufferSize < 0) {
                // use default buffer size
                readBufferSize = DEFAULT_READ_BUFFER_SIZE;
            }
    
            // if content length was set, read will be done in a single 
            // iteration because buffer fits... 
            StringBuffer response = new StringBuffer();
            char[] readBuffer = new char[readBufferSize];
            int len;
            while ((len = reader.read(readBuffer)) > 0) {
                response.append(new String(readBuffer, 0, len));                    
            }
            return response.toString();
    
    
    
        } catch (IOException ioe) {
            throw ioe;      
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Throwable t) {
                    System.out.println("readResponse: reader.close(), Class: Client");
                    //log
                }
            }
        }
    }
    
    /**
     * 
     * @return the url
     */
    public URL getURL() {
        return url;
    }
    

    }

0 个答案:

没有答案