Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   JAVA Networking: Code with getInputStream() was compatible with Java1.6, is but anymore with 1.7 (http://www.velocityreviews.com/forums/t959229-java-networking-code-with-getinputstream-was-compatible-with-java1-6-is-but-anymore-with-1-7-a.html)

musart 03-30-2013 09:22 PM

JAVA Networking: Code with getInputStream() was compatible with Java1.6, is but anymore with 1.7
 
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;
}
}

Steven Simpson 03-31-2013 12:00 AM

Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7
 
On 30/03/13 21:22, musart wrote:
> 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.


--
ss at comp dot lancs dot ac dot uk


Daniel Pitts 03-31-2013 02:38 AM

Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7
 
On 3/30/13 2:22 PM, musart wrote:
> 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.



Steven Simpson 03-31-2013 08:27 AM

Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7
 
On 31/03/13 00:00, Steven Simpson wrote:
> 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.


Of course I can; it's null if "the server sent no useful data".

<http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html#getErrorStream%28%29>

--
ss at comp dot lancs dot ac dot uk


marcos.barandun@gmail.com 03-31-2013 08:37 AM

Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7
 
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.www.protocol.http.HttpURLConnection$Ht tpInputStream@6f0ffb38
strResponse: *APPLETCOMMINFO08404905104805405004804805104905503 2084053048049032032032032050048048050049051101000_
default curser ether action: 4
-1
nullsun.net.www.protocol.http.HttpURLConnection$Ht tpInputStream@2be2befa
strResponse: *APPLETCOMMINFO08404905104805405004804805104905503 2084053048049032032032032050048048050049051101000_
default curser ether action: 0
-1
nullsun.net.www.protocol.http.HttpURLConnection$Ht tpInputStream@7e859a68
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.0 0000+02+0.00000+00+0.00000+00+0.00000+00+0.00000+0 0+0.00000+00+0.00000+00+0.00000+00+0.00000+00+0.00 000+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.000 00+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.0000 0+00+0.00000+00+0..00000+00+0.00000+00000010000200 00800192000000000000001000020000800192000000000000 00100002000080019200000000000000100002000080019200 00000000000000000000000000000000000001001001001002 00200200200300300300300000000000000100100100100000 00000000000000000000130130130130140140140140490570 56049032032032032192168001127255255255000000000000 00000100008000300000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000011111111111111111111111111111111000 00000000000000000000000000010020010021000000000000 00000011100000000000000000000000000000000000000000 00000000000001111111111000111110000063805_
default curser ether action: 0
-1
nullsun.net.www.protocol.http.HttpURLConnection$Ht tpInputStream@1fe903d5
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.0 0000+02+3.00000+02+3.00000+02+2.00000+02+2.00000+0 2+2.00000+02+2.00000+02+2.00000+02+2.00000+02+2.00 000+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.000 00+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.000 00+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.0000 0+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.0 0000+02+4.00000+02+4.00000+02+4.00000+02+4.00000+0 2+4.00000+02+4.00000+02+4.00000+02+4.00000+02+4.00 000+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.500 00+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.5000 0+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.0 0000+01+2.00000+01+2.00000+01+2.00000+01+2.00000+0 1+1.00000+02+1.00000+02+1.00000+02+1.00000+02+1.00 000+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.000 00+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.0000 0+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.0 0000+01+5.00000+01+5.00000+01+0.00000+00+0.00000+0 0+0.00000+00+0..00000+00+0.00000+00+0.00000+00+0.0 0000+00+0.00000+00+2.00000+03+2.00000+03+2.00000+0 3+2.00000+03+2.00000+03+2.00000+03+2.00000+03+2.00 000+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.000 00+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.0000 0+00+0.00000+00+0.00000+00000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000320320320320320320320320320 32032032032032032032049057055055032032032032000012 7981_
default curser ether action: 0
true

Kevin McMurtrie 03-31-2013 04:57 PM

Re: JAVA Networking: Code with getInputStream() was compatible with Java 1.6, is but anymore with 1.7
 
Post the received header here.
--
I will not see posts from Google because I must filter them as spam

telecaster 03-31-2013 05:16 PM

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:
*APPLETCOMMINFO08404905104805405004804805104905503 2084053048049032032032032050048048050049051101000_

marcos.barandun@gmail.com 03-31-2013 06:40 PM

Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7
 
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_

Roedy Green 04-01-2013 07:50 PM

Re: JAVA Networking: Code with getInputStream() was compatible with Java 1.6, is but anymore with 1.7
 
On Sun, 31 Mar 2013 01:37:20 -0700 (PDT), marcos.barandun@gmail.com
wrote, quoted or indirectly quoted someone who said :

>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

musart 04-02-2013 09:08 PM

Re: JAVA Networking: Code with getInputStream() was compatible withJava 1.6, is but anymore with 1.7
 
It worked


All times are GMT. The time now is 04:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.