Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JAVA Networking: Code with getInputStream() was compatible with Java 1.6, is but anymore with 1.7

132 views
Skip to first unread message

musart

unread,
Mar 30, 2013, 5:22:18 PM3/30/13
to
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;
}
}

Daniel Pitts

unread,
Mar 30, 2013, 10:38:24 PM3/30/13
to
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.


Message has been deleted

Kevin McMurtrie

unread,
Mar 31, 2013, 12:57:59 PM3/31/13
to
Post the received header here.
--
I will not see posts from Google because I must filter them as spam
Message has been deleted

Steven Simpson

unread,
Mar 30, 2013, 8:00:02 PM3/30/13
to
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

Steven Simpson

unread,
Mar 31, 2013, 4:27:36 AM3/31/13
to
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>

Roedy Green

unread,
Apr 1, 2013, 3:50:30 PM4/1/13
to
On Sun, 31 Mar 2013 01:37:20 -0700 (PDT), marcos....@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
Message has been deleted

Steven Simpson

unread,
Apr 3, 2013, 4:05:10 AM4/3/13
to
On 02/04/13 22:08, musart wrote:
> 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?

jorge

unread,
Apr 5, 2013, 12:11:46 PM4/5/13
to
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
0 new messages