HttpClient and POST requests

by fala70 » Sat, 22 Nov 2008 22:39:17 GMT


Sponsored Links
 Hi, I've a problem to translate follow code to org.apache.http.*;

           OutputStream pos = new OutputStream();
            pos.writeShort(Const.INVOCATION_CODE);
            pos.writeInt(requestId);
            pos.writeString(nickname);
            pos.writeString(password);

           conn = (HttpConnection) Connector.open(url,
Connector.READ_WRITE);
            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-Type", "application/octet-
stream");
            conn.setRequestProperty("Accept", "application/octet-stream");

            if(sessionCookie == null)
            {
                conn.setRequestProperty("version", "???");
            }
            else
            {
                conn.setRequestProperty("cookie", sessionCookie);
            }


            // Getting the output stream may flush the headers
            os = conn.openDataOutputStream();
            os.write(pos.getBytes());
            os.close();


start connection I must use :
                HttpClient conn = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

but I've difficult to understand what must I use to set the request
proprieties (setRequestProperty) and use an OutputStream to send my
requests

thanks
--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by Mark Murphy » Sat, 22 Nov 2008 22:47:04 GMT


 


Questions related to the Apache HTTPComponents are best asked of the 
Apache HTTPComponents team:

 http://hc.apache.org/ 

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 1.4 Published!

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


HttpClient and POST requests

by Adam Connors » Sun, 23 Nov 2008 00:08:47 GMT


 Hope this is useful:

   88:     HttpPost post = null;
   89:     post = new HttpPost(url);
   91:     post.addHeader("Content-Type", "multipart/related;
boundary=\"END_OF_PART\"");
   92:     post.addHeader("MIME-version", "1.0");
   93:
  116:
  117:     HttpEntity body = new ByteArrayEntity(bodyBytes);
  118:     post.setEntity(body);
  121:     HttpResponse resp = uploadClient.execute(post);

Instead of ByteArrayEntity sounds like you want InputStreamEntity or such
like...

*BUT: *In my experience using an InputStreamEntity is bad because over flaky
connections the HttpClient sometimes attempts to resend packets and the
InputStreamEntity is "not-repeatable"... (You'll get periodic
EntityNotRepeatable errors). Which is why I switched to using a
ByteArrayEntity as above which seems to work slightly better for large
streams.

Hope that answers your question,

Best,

Adam.





--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by fala70 » Sun, 23 Nov 2008 04:45:33 GMT


 Hi, I've a problem to translate follow code to org.apache.http.*;

           OutputStream pos = new OutputStream();
            pos.writeShort(Const.INVOCATION_CODE);
            pos.writeInt(requestId);
            pos.writeString(nickname);
            pos.writeString(password);

           conn = (HttpConnection) Connector.open(url,
Connector.READ_WRITE);
            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-Type", "application/octet-
stream");
            conn.setRequestProperty("Accept", "application/octet-stream");

            if(sessionCookie == null)
            {
                conn.setRequestProperty("version", "???");
            }
            else
            {
                conn.setRequestProperty("cookie", sessionCookie);
            }


            // Getting the output stream may flush the headers
            os = conn.openDataOutputStream();
            os.write(pos.getBytes());
            os.close();


start connection I must use :
                HttpClient conn = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

but I've difficult to understand what must I use to set the request
proprieties (setRequestProperty) and use an OutputStream to send my
requests

thanks

--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by fala70 » Sun, 23 Nov 2008 04:45:40 GMT


 I am using android-sdk-windows-1.0_r1 and need to translate a snip
code to use over android G1. Old is that:

           conn = (HttpConnection) Connector.open(url,
Connector.READ_WRITE);
            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-Type", "application/octet-
stream");
            conn.setRequestProperty("Accept", "application/octet-stream");

            if(sessionCookie == null)
            {
                conn.setRequestProperty("version", "???");
            }
            else
            {
                conn.setRequestProperty("cookie", sessionCookie);
            }


            // Getting the output stream may flush the headers
            os = conn.openDataOutputStream();
            os.write(pos.getBytes());
            os.close();


I must used org.apache.http.client.HttpClient; package. The new
connection type will be:
                HttpClient conn = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

but I didn't understand what use to translate setRequestProperty, for
set all my prorprieties.

thanks

--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by fala70 » Sun, 23 Nov 2008 07:24:32 GMT


 thanks Adam, for your answer. I tried your instructions but when I
call the execute I receive an IOException. Follow the stack log. Any
idea ???

Also I tried to use the same Header, but without success.
                httppost.addHeader("Content-Type", "application/octet-
stream");
                httppost.addHeader("Accept", "application/octet-stream");


stack log.

11-22 23:05:43.834: WARN/System.err(478): java.net.SocketException:
unknown error
11-22 23:05:43.843: WARN/System.err(478):     at
org.apache.harmony.luni.platform.OSNetworkSystem.createSocketImpl
(Native Method)
11-22 23:05:43.853: WARN/System.err(478):     at
org.apache.harmony.luni.platform.OSNetworkSystem.createSocket
(OSNetworkSystem.java:79)
11-22 23:05:43.853: WARN/System.err(478):     at
org.apache.harmony.luni.net.PlainSocketImpl2.create
(PlainSocketImpl2.java:59)
11-22 23:05:43.863: WARN/System.err(478):     at
java.net.Socket.checkClosedAndCreate(Socket.java:763)
11-22 23:05:43.873: WARN/System.err(478):     at
java.net.Socket.connect(Socket.java:910)
11-22 23:05:43.873: WARN/System.err(478):     at
org.apache.http.conn.scheme.PlainSocketFactory.connectSocket
(PlainSocketFactory.java:117)
11-22 23:05:43.882: WARN/System.err(478):     at
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection
(DefaultClientConnectionOperator.java:129)
11-22 23:05:43.892: WARN/System.err(478):     at
org.apache.http.impl.conn.AbstractPoolEntry.open
(AbstractPoolEntry.java:164)
11-22 23:05:43.892: WARN/System.err(478):     at
org.apache.http.impl.conn.AbstractPooledConnAdapter.open
(AbstractPooledConnAdapter.java:119)
11-22 23:05:43.904: WARN/System.err(478):     at
org.apache.http.impl.client.DefaultRequestDirector.execute
(DefaultRequestDirector.java:348)
11-22 23:05:43.914: WARN/System.err(478):     at
org.apache.http.impl.client.AbstractHttpClient.execute
(AbstractHttpClient.java:555)
11-22 23:05:43.914: WARN/System.err(478):     at
org.apache.http.impl.client.AbstractHttpClient.execute
(AbstractHttpClient.java:487)
11-22 23:05:43.923: WARN/System.err(478):     at
org.apache.http.impl.client.AbstractHttpClient.execute
(AbstractHttpClient.java:465)

--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by Adam Connors » Sun, 23 Nov 2008 17:36:38 GMT


 Best guess... Do you have the INTERNET permission correctly declared
in your manifest...?




--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by Guillaume Perrot » Sun, 23 Nov 2008 19:34:03 GMT


  http://hc.apache.org/httpcomponents-client/examples.html 



--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by fala70 » Sun, 23 Nov 2008 22:09:47 GMT


 Yeah.... Thanks Adam now work




--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by Christine » Mon, 24 Nov 2008 05:56:16 GMT


 The examples that Guillaume points you to cover most of what you want
to do in a POST.
What they don't cover is multipart mime messages and setting mime
types. With "application/octet" as the mime type you should be ok with
the examples. Otherwise, do have a look at HttpMime.

On Nov 23, 12:33pm, Guillaume Perrot <[EMAIL PROTECTED]>



--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by agal » Thu, 18 Dec 2008 01:26:35 GMT


 i,

I would like to know how to set headers in an Http Post request. This
is what I tried:
...
HttpPost postMethod=new HttpPost(posturl);
postMethod.addHeader("Content-Type", "something");
postMethod.addHeader("Content-Length", "length of content");
String requestBody = "some text";
postMethod.setEntity(new StringEntity(requestBody));
...

The server that I'm posting to is rejecting this request because it's
not able to retrieve the content length.

Could anyone help me with this?

Thanks.


On Nov 23, 7:09pm, fala70 <fal...@gmail.com> wrote:

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Can we play a recorded voice on phone call.

Hello,
         Is that possible to play a recorded voice on a phone call.
Like as answering machine does. ???

Can somebody help me ??

Thanks
Abhinav Parashar
--~--~---------~--~----~------------~-------~--~----~

2. Is it possible to load to activities in the same screen each for upper or lower part of the screen?

Is it possible to load to activities in the same screen each for upper
or lower part of the screen?
both works independentfrom each other ...
--~--~---------~--~----~------------~-------~--~----~

3. How to create a thread that waits for an answer from a certain port ( from external server ) ?

4. How to create a thread that waits for an answer from a certain port ( from external server ) ?

5. NullPointerException when trying to make a button action??

6. Main Menu (Very Easy)

7. Is it possible to load a view from an XML file, but only to store it to a variable?