JAVA Networking: Code with getInputStream() was compatible with Java1.6, is but anymore with 1.7


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

Advertisements

S

Steven Simpson

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.

Get the menu on one of the HTTP-carrying frames, and select "Follow TCP
Stream". What is the result? Does it look like a proper HTTP exchange?

I would suggest calling getErrorStream() if you get an exception from
getInputStream(), but if the response does not appear to be valid HTTP,
I can't predict what getErrorStream() will actually give you.
 
D

Daniel Pitts

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
does the "status" variable print out? It would if you made it to the
getInputStream call. What is its value? Is it a valid HTTP value? If
not, then the server is doing it wrong and you can insist that is the
case. Broken server is broken.
 
M

marcos.barandun

Status variable (int status = connection.getResponseCode()) is always -1 as seen by the console output either with Java 1.6 or 1.7, but with Java 1.6 the Client-Server communication works fine.


console output with Java 1.7:
-1
error sending get request http://192.168.1.127:80/ string: *APPLETCOMMINFO_
strResponse: Ethernet Exception


console output with Java 1.6:
-1
nullsun.net.[email protected]
strResponse: *APPLETCOMMINFO084049051048054050048048051049055032084053048049032032032032050048048050049051101000_
default curser ether action: 4
-1
nullsun.net.[email protected]
strResponse: *APPLETCOMMINFO084049051048054050048048051049055032084053048049032032032032050048048050049051101000_
default curser ether action: 0
-1
nullsun.net.[email protected]
strResponse: *APPLETCOMMRDKP+1.00000+00+1.00000+00+1.00000+00+1.00000+00+1.00000+00+1.00000+00+1.00000+00+1.00000+00+3.50000+01+5.00000-01+3.50000+01+5.00000-01+3.50000+00+1.00000+02+1.00000+02+1.00000+02+1.00000+02+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0..00000+00+0.00000+0000001000020000800192000000000000001000020000800192000000000000001000020000800192000000000000001000020000800192000000000000000000000000000000000000000100100100100200200200200300300300300000000000000100100100100000000000000000000000001301301301301401401401404905705604903203203203219216800112725525525500000000000000000100008000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111000000000000000000000000000000100200100210000000000000000001110000000000000000000000000000000000000000000000000000001111111111000111110000063805_
default curser ether action: 0
-1
nullsun.net.[email protected]
strResponse: *APPLETCOMMRDPP+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+3.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2..00000+02+2.00000+02+2.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+5.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+02+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2.50000+01+2..00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+01+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00000+02+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+5.00000+01+0.00000+00+0.00000+00+0.00000+00+0..00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+2.00000+03+2.00000+03+2.00000+03+2.00000+03+2.00000+03+2.00000+03+2.00000+03+2.00000+03+0.00000+00+0.00000+00+0.00000+00+0.00000+00+1.00000+02+1.00000+02+1.00000+02+1.00000+02+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00000+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000320320320320320320320320320320320320320320320320490570550550320320320320000127981_
default curser ether action: 0
true
 
Ad

Advertisements

Joined
Mar 31, 2013
Messages
2
Reaction score
0
Header captured with Wireshark

HEADER CAPTURED WITH WIRESHARK (WITH BOTH JRE 1.6 & 1.7 I send and receive the same)

SENT:
GET /*APPLETCOMMINFO_ HTTP/1.1

Cache-Control: no-cache

Pragma: no-cache

User-Agent: Java/1.6.0_43

Host: 192.168.1.127

Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

Connection: keep-alive



RECEIVED:
*APPLETCOMMINFO084049051048054050048048051049055032084053048049032032032032050048048050049051101000_
 
M

marcos.barandun

HEADER CAPTURED WITH WIRESHARK (WITH BOTH JRE 1.6 & 1.7 I send and receive the same)

SENT:
GET /*APPLETCOMMINFO_ HTTP/1.1

Cache-Control: no-cache

Pragma: no-cache

User-Agent: Java/1.6.0_43

Host: 192.168.1.127

Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

Connection: keep-alive



RECEIVED:
*APPLETCOMMINFO08404905104805405004804805104905503 2084053048049032032032032050048048050049051101000_
 
R

Roedy Green

Status variable (int status =3D connection.getResponseCode()) is always -1 =

-1 is what happens when you get no response whatsoever. Try cranking
up the timeout. Retry. Check that DNS is working. Try probe using IP.
Make sure this is not redirected to https: If so anything not kosher
with cert or list of CAs in cacerts will derail you.
--
Roedy Green Canadian Mind Products http://mindprod.com
Motors make noise, and that tells you about the feelings and attitudes
that went into it. Something was more important than sensory pleasure --
nobody would invent a chair or dish that smelled bad or that made horrible
noises -- why were motors invented noisy? How could they possibly be
considered complete or successful inventions with this glaring defect?
Unless, of course, the aggressive, hostile, assaultive sound actually served
to express some impulse of the owner.
~ Philip Slater (born: 1927 age: 85)
The Wayward Gate: Science and the Supernatural
 
Joined
Mar 31, 2013
Messages
2
Reaction score
0
Resolved

Finally resolved thanks to marcos.barandun who sent me a piece of code which worked like a charm. Thanks mate!!!
 
Ad

Advertisements

S

Steven Simpson

It worked

What worked? The information you gave suggested that the server was at
fault for not obeying HTTP, yet fixing the server was not an option for
you. Did you drop HttpURLConnection and HTTP altogether? Did
getErrorStream provide the response despite its promise of returning
null if the server sent "no useful data"? Did the problem suddenly
de-materialize?
 
Ad

Advertisements

J

jorge

Pat?tico tio ni puta idea de inform?tica, ded?acate a otra cosa imb?cil que bastante verguenza ajena est?s dando. Pat?tico

Pathetic guy fucking clue about computers, another thing ded?acate moron who embarrassed enough're giving. pathetic

Pathetic guy fucking Ahnung ?ber Computer, ded?acate andere Sache Idiot, die enough're geben verlegen. armselig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top