1

我有一个在 PC 和机器之间建立通信的软件,它曾经在 Java 1.6 中工作,但在 1.7 中不再适用。IOException --> "Invalid Http response" 是我在调用 getInputStream() 方法后立即得到的。似乎该方法得到了改进并且更加敏感,这意味着在 Java 1.6 中没有特别检查 responseCode= -1 结果。

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

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

对于如何修改或实现新的东西以使代码与版本兼容,我将不胜感激。1.7 因为我不是前程序员...谢谢

序列:

  1. get 被称为
  2. 返回 readResponse(..) 被调用
  3. getInputStream() --> IOException
  4. catch (Exception e) {System.out.println("发送get请求出错" + getURL() + " string: " + requestString); 返回错误。以太网异常;//去做

这里是控制台输出:

-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;
}

}

4

0 回答 0