ios - Send POST to HTML website -
i'm searching best solution send data html website in app. got variables in program in following format:
1 var1 2 var2 3 var3 4 var4 5 var5 and on. nothing more that. best way send website?
following code describe simple example post method.(how can pass data post method)
you can use following code snippet, described in article:
here, simple describe how can use of post method.
1. set post string actual username , password.
nsstring *post = [nsstring stringwithformat:@"&username=%@&password=%@",@"username",@"password"];2. encode post string using
nsasciistringencoding, post string need send in nsdata format.nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes];you need send actual length of data. calculate length of post string.
nsstring *postlength = [nsstring stringwithformat:@"%d",[postdata length]];3. create urlrequest properties
httpmethod, http header field length of post string. createurlrequestobject , initialize it.nsmutableurlrequest *request = [[[nsmutableurlrequest alloc] init] autorelease];set url going send data request.
[request seturl:[nsurl urlwithstring:[nsstring stringwithformat:@"http://www.abcde.com/xyz/login.aspx"]]];now, set http method (post or get). write lines in code.
[request sethttpmethod:@"post"];set
httpheader field length of post data.[request setvalue:postlength forhttpheaderfield:@"content-length"];also set encoded value http header field.
[request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"current-type"];set
httpbodyof urlrequest postdata.[request sethttpbody:postdata];4. now, create urlconnection object. initialize urlrequest.
nsurlconnection *conn = [[nsurlconnection alloc]initwithrequest:request delegate:self];it returns initialized url connection , begins load data url request. can check whether
urlconnection done or not using if/else statement below.if(conn) { nslog(@”connection successful”) } else { nslog(@”connection not made”); }5. receive data http request , can use delegate methods provided urlconnection class reference. delegate methods below.
- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata*)dataabove method used receive data using post method.
- (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)errorthis method , can use receive error report in case of connection not made server.
- (void)connectiondidfinishloading:(nsurlconnection *)connectionthe above method used process data after connection has made successfully.
also refer this and this documentation post method.
and here best example source code of httppost method.
Comments
Post a Comment