"stuck "on the getResponseCode() line

C

Camel

Hi all,
I have an application that uses a communication Thread that opens a
connection to a server(router's web server) every few seconds. I'm using a
simple http protocol. Below is the piece of code:

------code begin--------
......
URL gatewayURL;
HttpURLConnection gc = null;
DataOutputStream dos = null;
String gatewayStr = http://192.168.0.1/cgi-bin/webcm;
String query = URLEncoder.encode("logic:command/save","UTF-8");
query += "=";
query += URLEncoder.encode("","UTF-8");
try {
gatewayURL = new URL(gatewayStr);

gc = (HttpURLConnection)gatewayURL.openConnection();

gc.setDoOutput(true);
gc.setDoInput(true);
gc.setAllowUserInteraction(false);
gc.setUseCaches(false);
gc.setRequestMethod("POST");

gc.connect();

dos = new DataOutputStream(gc.getOutputStream());
dos.writeBytes(query);
dos.flush();

int code = gc.getResponseCode();
String message = gc.getResponseMessage();

System.out.println(gc.getURL());
System.out.println(code + " " + message);

dos.close();
gc.disconnect();
}catch (Exception e) {}

......

------code end------

The problem is: sometimes the program stops at gc.getResponseCode() line. No
exception cought. Nothing, it just stops at there.

What could be the problem?

Thank you all in advance.
 
K

Kevin McMurtrie

Camel said:
Hi all,
I have an application that uses a communication Thread that opens a
connection to a server(router's web server) every few seconds. I'm using a
simple http protocol. Below is the piece of code:

------code begin--------
......
URL gatewayURL;
HttpURLConnection gc = null;
DataOutputStream dos = null;
String gatewayStr = http://192.168.0.1/cgi-bin/webcm;
String query = URLEncoder.encode("logic:command/save","UTF-8");
query += "=";
query += URLEncoder.encode("","UTF-8");
try {
gatewayURL = new URL(gatewayStr);

gc = (HttpURLConnection)gatewayURL.openConnection();

gc.setDoOutput(true);
gc.setDoInput(true);
gc.setAllowUserInteraction(false);
gc.setUseCaches(false);
gc.setRequestMethod("POST");

gc.connect();

dos = new DataOutputStream(gc.getOutputStream());
dos.writeBytes(query);
dos.flush();

int code = gc.getResponseCode();
String message = gc.getResponseMessage();

System.out.println(gc.getURL());
System.out.println(code + " " + message);

dos.close();
gc.disconnect();
}catch (Exception e) {}

......

------code end------

The problem is: sometimes the program stops at gc.getResponseCode() line. No
exception cought. Nothing, it just stops at there.

What could be the problem?

Thank you all in advance.

You need to close the output stream rather than flush it. POST needs
some kind of stream termination at the HTTP level so the reader knows
when the data has been fully read. If using the chunked transfer
encoding, the terminator (empty chunk) must be sent. If using a
Content-Length header, the internal buffer needs to be sized and
written. Your program gets stuck because the server is waiting for more
data.
 

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top