ios - Convert a Web service from GET to POST -
i have created web service create communication between ios application , joomla web site, , used method communicate between mobile application , web service , between web service , controller (php file work , homecoming data) , didn't find how convert implementation post method here actual scheme :
ws.php : it's web service (simple illustration )
<?php $id = $_get['id'] ; // info url // here create testes // redirect controller of joomla component receive // phone call of request url attribute "action" of // existing html form implement login system, added // parameter called web service help me modify controller // create difference between normal phone call , web service phone call header("location: index.php?option=com_comprofiler&task=login&ws=1&id=1"); ?>
controller.php : receiver of web service phone call , web phone call (from browser)
<?php // code added me, in existent controller of component // waiting form submitting, got convert info post here if (isset($_get['ws'])) // it's web service phone call { $_post['id'] = $_get['id'] ; // task ... if ($correctlogin) // illustration echo "1" else echo '0'; } ?>
i didn't set real implementation, , it's simple illustration of system, it's same
call mobile
nsurl *url = [[nsurl alloc]initwithstring:@"http://localhost/ws.php?id=1"]; nsdata *dataurl= [nsdata datawithcontentsofurl:url]; nsstring *str = [[nsstring alloc]initwithdata:dataurl encoding:nsutf8stringencoding]; if(![str isequaltostring:@"0"]) nslog(@"connected"); else nslog(@"not connected");
so don't want utilize method, want receive info mobile using post , send info controller using post also, best solution ?
if want app send info using post method, i'm code. hope help.
it takes info sent in dictionary object. ecodes info sent post , returns response (if want results in string format can utilize [[nsstring alloc] initwithdata:dresponse encoding: nsasciistringencoding]; when returning data)
-(nsdata*) getdata:(nsdictionary *) postdatadic{ nsdata *dresponse = [[nsdata alloc] init]; nsurl *nurl = [nsurl urlwithstring:url]; nsdictionary *postdict = [[nsdictionary alloc] initwithdictionary:postdatadic]; nsdata *postdata = [self encodedictionary:postdict]; // create request nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:nurl]; [request sethttpmethod:@"post"]; // define method type [request setvalue:[nsstring stringwithformat:@"%d", postdata.length] forhttpheaderfield:@"content-length"]; [request setvalue:@"application/x-www-form-urlencoded charset=utf-8" forhttpheaderfield:@"content-type"]; [request sethttpbody:postdata]; // peform request nsurlresponse *response; nserror *error = nil; dresponse = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; homecoming dresponse; }
this method prepares dictionary info post
- (nsdata*)encodedictionary:(nsdictionary*)dictionary { nsmutablearray *parts = [[nsmutablearray alloc] init]; (nsstring *key in dictionary) { nsstring *encodedvalue = [[dictionary objectforkey:key] stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; nsstring *encodedkey = [key stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; nsstring *part = [nsstring stringwithformat: @"%@=%@", encodedkey, encodedvalue]; [parts addobject:part]; } nsstring *encodeddictionary = [parts componentsjoinedbystring:@"&"]; homecoming [encodeddictionary datausingencoding:nsutf8stringencoding]; }
ios web-services post joomla get
No comments:
Post a Comment