This question already has answers here:
When should I use GET or POST method? What's the difference between them?
(15 answers)
Closed 9 years ago.
Can anyone tell me difference between Httpost and Httpget method.Httpclient can execute POST and GET method.Please explain the difference between 1 and 2nd method
1.)httpclient.execute(httppost)
and the other one
2.) httpclient.execute(httpget)
In Http Get Method all the values that user sends to the server, embedded with the URL that we send as the request, where a third party can easily watch it. If I say it more technical way, by using the HTTP GET method, we are sending user details in the header part of the request not in the body.
source
Where as in Http Post method we transfer confidential data to a different location by using HTTP. The reason is the content goes inside the body, not with the header as in GET method.
source
You can also refer android developers website
The Apache HTTP Client supports all the commands that are supported by HTTP, such as GET, PUT, POST, DELETE, TRACE, OPTIONS & HEAD.
Most well-known are GET & POST, where GET is used to fetch a resource from the URL and POST is used to store data at a URL. There are official ways in how the different methods should be used, especially when creating a REST API, but in real life most boil down to the GET and the POST.
For a full discussion of the different methods you can read RFC2616 describing the HTTP: http://www.faqs.org/rfcs/rfc2616.html
Related
This question already has answers here:
What is the difference between POST and GET? [duplicate]
(7 answers)
Closed 4 years ago.
I am using android studio and got me wondering what's the difference of using post and get method request. I know when we use GET METHOD as a request in front end, we would see the request parameters in the url. But in android there aren't any url, so what difference does it make when using post method and get method in android ?
Well, The main difference is GET passes request parameter in URL string while POST passes request parameter in request message body which makes it more secure way of transferring data from client to server in http protocol
But if you look more into it, there are any other difference:
First
GET is used for data retrieval only. you can refine what you get from GET METHOD but it is as read only.
While POST is used for send data, But it only like a way to break the simple workings of HTML because you are neither guaranteed of anything that is happening, while it can fetch, send, or delete a data.
Second
GET request can only pass limited amount of data while POST method can pass large amount of data to server
Third
GET is mostly used for view purpose (e.g. SQL SELECT) while POST is mainly use for update purpose (e.g. SQL INSERT or UPDATE).
Fourth
the result of POST may not result in an actual page.
and Lastly
If you have a HIDDEN inputs in form then submitting a GET request reveals that HIDDEN inputs. (PHP GET and POST)
My client is an Android app and my service is an asp.net web api. I’m the only one using my service. I am trying to duplicate, in the Android-REST world what I am already doing in the Microsoft Windows Phone 7/ WCF SOAP world. I have numerous methods that both receive complex objects and return complex objects.
The WCF-SOAP world is simple. You can pass any complex arguments you want and return any complex results you want. Logically, it’s just a Remote Procedure Call.
But when I post questions about doing the same thing in REST, I’m told I should limit my services into GET, PUT, POST, and DELETE only. And that I should only do what is “proper” according to RFC2616. Some speak of this in almost a religious manner.
Forgeting about the religion, what’s wrong with using a GET for everything? Or what’s wrong with using a POST for everything? What I do does not fall into the simplistic RFC2616 categories. For instance I’m passing a thousand legs of a trip taken in a car and I’m getting back another version of that trip with erratic GPS errors smoothed out. Or, I’m sending a conversation in english and getting that conversation back in German.
In the android client I have the objects I want to send over HTTP already serialized into json strings by using Google-GSON. So my questions are…
How can I send these json strings to my REST Service as arguments in either GET or POST?
Is it possible and feasible to use just all GETs (or all POSTs) for all of my calls to my REST Service and how do I do that?
I have a more pragmatic question about this posted at sending a json string in a http url as I can’t find any examples anywhere of sending json strings over http GET or POST.
Thanks, Gary
Using the good HTTP verb is very usefull to simply know what to do when you request failed (for example) or just to do some specific stuff. If you sent a POST request, it's implicitly suppose that you have to parse your resource in order to obtain a stream which be sent via the request's body. In other hand, when you are retrieving data via GET, it's suppose that the request is gonna be sent back to you as a stream that you will mapped to your model, pojo, or anything else.
I can suggest you to use library such as RESTDroid. You can send POJO and receive POJO. It's a "resource oriented" library, so you can know at any moment if a particular local resource is remotely syncronized. Data persistence between local and remote is automatically handles.
RESTDroid is alpha released. You can have a look to RoboSpice. It's a powerful library to manage REST call but it's up to you to manage the persistency between local and remote resources.
1) The WCF-SOAP world is simple. You can pass any complex arguments you want and return any complex results you want. Logically, it’s just a Remote Procedure Call.
- IN REST:"You can pass any complex arguments you want and return any complex results you want too.
2a) Forgeting about the religion, what’s wrong with using a GET for everything?
In SOAP services WCF/or classical you are wrapping all requests into http POST so using single verb would end up to SOAP or - don't even think about it - your own communication protocol:-D
2b) You can technically compose GET request with non empty body - most of the servers ignore it by default though and it would be technically problematic to read it..
the other part of the question is answered by Pcriulan above
Hi i am new to android programming and i would like to know how to call a web service and pass parameters to the web service to obtain the return result in form of an XML.
I previously used Ksoap lib and SOAP request but thats not working correct so now i am trying the HTTP method.
Please Help
You will want to read up on the Apache HttpClient.
Beyond that its a matter of making sure the device and server are generating messages that the other side can correctly parse.
Is the server an existing service? Or are you designing that as well?
check the following in which you will use HTTP post request & you will get response in the form on XML
Android HttpPost: how to get the result
I am currently working on building an Android app for my Rails backend.
I created api_tokens_controller to create an api_token when user logs in from Android using HttpPost and save that token both in Rails user table and Android shared preferences. I was able to verify this.
I am trying to send the api_token as params[:api_token] from my Android app with every subsequent request after initial log in and have Rails verify:
In User.rb:
def self.authenticate_with_api_token(api_token_from_mobile)
user = find_by_api_token(api_token_from_mobile)
if user.nil?
return nil
else
return user
end
end
if user exists, then send other data as JSONObjects.
I am running into trouble conceptually here. I am now trying to have the Android app go to
...:3000/users and HttpGet user data as JSONObjects. The problem is that I don't know how to send the api_token from Android app to Rails in HttpGet. I am assuming that since it is RESTful, my HttpRequests have to correspond to the way rails routes work.
Questions:
Is it possible to send in JSONObject of params[:api_token] from android app to Rails in HttpGet?
Do my HttpRequests have to correspond to the RESTful way Rails works or can I just only use HttpPost?
Thanks
If I understand you correctly, then you just want to add the API token to as a URL parameter if using GET, so something like:
...:3000/users.json?api_token=token
This will put the api_token in the params hash just the same as POST data would be. The RESTful way would be to use GET for retrieving data from the API and using POST to send data, i.e. changing data on your backend but if you are going to be returning a user with the request then GET is the correct method to be using anyway.
I'm a former Ruby on Rails developer myself.
I agree with Tom that you can just pass in the api_token as a parameter with an HTTP GET.
For your second question, I'd say that the RESTful way is to use all the HTTP Verbs for the different actions that you can take in the API. You could change your routes to use HTTP POST and then supply extra parameters to differentiate between say Create and Update (to emulate a PUT). However, I don't think that's the way forward.
However, the good news is that whether you use HttpClient or HttpUrlConnection in your Android app, you will have access to the different HTTP verbs you need. For HttpClient, it is actually different classes like HttpGet, HttpPost, HttpPut and HttpDelete which you then configure by supplying URL and other parameters. For HttpUrlConnection, you just call it like so:
setRequestMethod("PUT");
Here's the relevant documentation on HttpUrlConnection:
HttpURLConnection uses the GET method by default. It will use POST if setDoOutput(true) has been called. Other HTTP methods (OPTIONS, HEAD, PUT, DELETE and TRACE) can be used with setRequestMethod(String).
I only want to send an html form by post method to a https resource from an Android activity.
I have (only for development and testing) an Lighttpd server with it own certificate to make handshaking, so there MUST happen authentication at least from server (client authentication is optional but desirable).
I have seen, lots of forums with different ways to make it, but I am a little confused, I do not know which could be the correct way to make it.
Please show me a chunk of code.
Thank you very very much.
Try out this tutorial, it shows GET, POST and Multipart POST request on the android platform
http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
I wrote some code to submit an HTML form to a server over https, which can be found in this answer. The version in the answer uses an HttpsUrlConnection, and the version in the question uses HttpClient. I could never quite get the right result from the server with the HttpClient version, but either approach should work in theory.