J2ME HttpConnection OutputStream won't write

E

Endi

Hi all,

does anybody know a solution to this:

I try to write data from a handheld via http to a webserver. But
somehow the server seems to ignore my OutputStream. I can open the
connection and receive the response, but when I try to write something
the server sends no response. The function getResponseCode() gives me a
200 but in the next step the InputStream is empty. I can send the post
command "POST http://1.10.1.5/webtest.nsf/Zaehler?CreateDocument
Http/1.1" via telnet and the server works fine but not from the
handheld. Please help. Thanks in advance.

Regards

Andreas

try{

HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
int rc;

try {

c = (HttpConnection)Connector.open("http://10.10.1.5");

c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("If-Modified-Since",
"29 Oct 1999 19:43:31 GMT");
c.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
c.setRequestProperty("Host", "10.10.1.5");
c.setRequestProperty("Accept", "text/html");
c.setRequestProperty("Connection", "close");
String s =
"http://1.10.1.5/webtest.nsf/Zaehler?CreateDocument";
int l = s.length();
StringBuffer sb = new StringBuffer();
sb.append(l);
c.setRequestProperty("Content-Length", sb.toString() );


is = c.openInputStream();

rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}

int len = (int)c.getLength();
if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}
// process(data);
} else {
int ch;
while ((ch = is.read()) != -1) {
// process((byte)ch);
}
}

os = c.openOutputStream();
os.write(s.getBytes());
os.flush();
rc = 0;
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}

len = (int)c.getLength();
if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}
// process(data);
} else {
int ch;
while ((ch = is.read()) != -1) {
// process((byte)ch);
}
}

} catch (ClassCastException e) {...
 
J

JScoobyCed

Endi said:
Hi all,

does anybody know a solution to this:

I try to write data from a handheld via http to a webserver. But
somehow the server seems to ignore my OutputStream. I can open the
connection and receive the response, but when I try to write something
the server sends no response. The function getResponseCode() gives me a
200 but in the next step the InputStream is empty. I can send the post
command "POST http://1.10.1.5/webtest.nsf/Zaehler?CreateDocument
Http/1.1" via telnet and the server works fine but not from the
handheld. Please help. Thanks in advance.

Regards

Andreas

I think you try to send data to a closed connection. In your code, you
set the property:
c.setRequestProperty("Connection", "close");
This means that the server will close the connection as soon as it has
sent its data.
You should try to use
c.setRequestProperty("Connection", "Keep-Alive");
(not sure the syntax)
instead.

Additionally, can you tell what is in the "s" String that you want to
write back to the server?
os = c.openOutputStream();
os.write(s.getBytes());
os.flush();

If the connection is set to stay open, you can reuse it to send a HTTP
formatted request. That means the "s" String has to be a valid HTTP
request (GET, POST, CGI, ...), or else the server will return an
"HTTP/1.1 400 Bad Request" response.

Hope this help
 
E

Endi

Hi JScoobyCed,

thanx for your reply. I had the Keep-Alive parameter for the connection
before. And it didn't work either. The s String contains the post
commands that will be sent to a server when you click the submit button
of the following html-file:

<html><body>
<form method="post"
action="http://10.10.1.5/webtest.nsf/V_Zaehler/Z1?SaveDocument"
name="_Zaehler">
<input name="Vorname" value="www">
<input type="submit" value="">
</form></body></html>

I thought what I write to the outputstreams are the post commands that
the server expects when you click on the submit button of a form. But I
think I am wrong.

Andreas
 
D

Darryl Pierce

Endi wrote:
<snip>

You're opening the input stream before it's been realized. You can only
open it *after* you're gotten the response code from the server. ANd,
you've never written anything to the *OUTPUT STREAM* for your request,
so no data was ever sent to the server.
 
D

Darryl Pierce

JScoobyCed said:
I think you try to send data to a closed connection. In your code, you
set the property:
This means that the server will close the connection as soon as it has
sent its data.
You should try to use
(not sure the syntax)
instead.

No need for that. If you don't set a Connection property, then *by
default* it's set to keep-alive (MIDP uses a subset of HTTP 1.1, which
has persistent connections by default).
Additionally, can you tell what is in the "s" String that you want to
write back to the server?

If the connection is set to stay open, you can reuse it to send a HTTP
formatted request.

But, you cannot reuse the HttpConnection *object*. It's a stateful
object that, once it's received its request, cannot be reused to send
another request.
 
D

Darryl Pierce

Endi said:
thanx for your reply. I had the Keep-Alive parameter for the connection
before. And it didn't work either. The s String contains the post
commands that will be sent to a server when you click the submit button
of the following html-file:

You're doing your entire connection wrong. You should:

1. Create an HttpConnection object using Connector.open(String)
2. Define the request parameters, including content type and length
3. Open the output stream on the connection to write your data
4. Call the connection's getResponseCode() method, which will send the
request to the server and get the response
5. Call the connections getInputStream() method to retrieve the data
from the response
6. If you need to do another round trip with the server, go to step 1.
above and create a *new* HttpConnection object. The underlying
implementation will re-use the *physical connection* to the server if
that's an option. You *cannot* reuse your HttpConnection object to do a
second roundtrip with the server.
 

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

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top