ok struggling build httppost thing correctly... built asp.net web api mvc 4, , trying pull data 1 of controllers in android app. code in android (java) not know how write correctly interface asp.net (like headers, namevaluepairs, etc). post controller code well.
defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(http://proopt.co.za.winhost.wa.co.za/api/course); list<namevaluepair> namevalue = new arraylist<namevaluepair>(); namevalue.add(new basicnamevaluepair("", "")); httppost.setentity(new urlencodedformentity(namevalue)); httppost.setheader("content-type", "application/json"); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity();
and controller follows:
// api/course/5 public course getcourse(string id) { course course = db.courses.find(id); if (course == null) { throw new httpresponseexception(request.createresponse(httpstatuscode.notfound)); } return course; }
the url use http post http://proopt.co.za.winhost.wa.co.za/api/course
please assist me, thanks.
update 2017
use restful api interface remote databases. client app should make use of token-based authentication, , retrofit 2.0 fantastic library consuming remote rest apis.
in java code using httppost
in aps.net/mvc website have posted getcourse
"get" action. mvc controller actions, have add [httppost]
(or rather [httppost, actionname("create")]
avoid naming conflicts same named action) attribute
but doesn't comply restful design. obtaining data should use method. use "post" when resources being updated or created (i.e. deleting, updating, inserting) , "put" replace them. if work javascript (i.e. website) use post instead of put, since javascript can handle post , get.
so use httpget
in javacode , remove namevaluepair
stuff it's not required in get. other coude seems fine, using operation change resources.