M
musart
Hello Everybody...
I have a piece of software which establishes a communication between a PC and a machine, which used to work in Java 1.6, but doesn't anymore in 1.7. The IOException --> "Invalid Http response" is what I get as soon as the getInputStream() method is called. It seems like the method had been improved an is much more sensitive, meaning that responseCode=-1 result is not specifically checked in Java 1.6.
Assited with Wireshark I checked if both versions (1.6 & 1.7) sent and received same Ethernet frames, and so did both.
I can only resolve this from the PC (client) side...that means no posibility to edit or change the Server code.
I would apreciate any help on how to modify or implement something new to make the code compatible for ver. 1.7 as Im not a former programmer... Thanks
1. get is called
2. return readResponse(..) is called
3. getInputStream() --> IOException
4. catch (Exception e) {System.out.println("error sending get request " + getURL() + " string: " + requestString); return Error.ethernetException; //TODO
Code:
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;
}
}
I have a piece of software which establishes a communication between a PC and a machine, which used to work in Java 1.6, but doesn't anymore in 1.7. The IOException --> "Invalid Http response" is what I get as soon as the getInputStream() method is called. It seems like the method had been improved an is much more sensitive, meaning that responseCode=-1 result is not specifically checked in Java 1.6.
Assited with Wireshark I checked if both versions (1.6 & 1.7) sent and received same Ethernet frames, and so did both.
I can only resolve this from the PC (client) side...that means no posibility to edit or change the Server code.
I would apreciate any help on how to modify or implement something new to make the code compatible for ver. 1.7 as Im not a former programmer... Thanks
1. get is called
2. return readResponse(..) is called
3. getInputStream() --> IOException
4. catch (Exception e) {System.out.println("error sending get request " + getURL() + " string: " + requestString); return Error.ethernetException; //TODO
Code:
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;
}
}