Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

J2ME HttpConnection OutputStream won't write

Thread view: 
Endi - 14 Jun 2005 16:31 GMT
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) {...
JScoobyCed - 15 Jun 2005 10:27 GMT
> Hi all,
>
[quoted text clipped - 12 lines]
>
> 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

--
JSC
Endi - 16 Jun 2005 15:05 GMT
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
Darryl Pierce - 26 Jun 2005 15:17 GMT
> 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.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: http://mcpierce.multiply.com
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard

Darryl Pierce - 26 Jun 2005 15:15 GMT
> I think you try to send data to a closed connection. In your code, you
> set the property:
[quoted text clipped - 5 lines]
> (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?
[quoted text clipped - 4 lines]
> 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.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: http://mcpierce.multiply.com
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard

Darryl Pierce - 26 Jun 2005 15:12 GMT
<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.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: http://mcpierce.multiply.com
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.