objective c - How I know the the posting data goes correctly -
-(ibaction)clicked:(id)sender{ nsstring *cidstring = cid.text; nsurl *url = [nsurl urlwithstring:@"http://localhost:8080/test/?"]; nsstring *postdata = [nsstring stringwithformat:@"companyid=%@",cidstring]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[url standardizedurl] cachepolicy:nsurlrequestreloadignoringlocalcachedata timeoutinterval:60]; [request sethttpmethod:@"post"]; [request setvalue:@"application/x-www-form-urlencoded; charset=utf-8" forhttpheaderfield:@"content-type"]; [request sethttpbody:[postdata datausingencoding:nsutf8stringencoding]]; [self startconnection:(nsmutableurlrequest *)request]; if([self.result isequaltostring:@"new alert"]) { cid.text = @"scuess"; } }
where startconnection method follows
- (void)startconnection:(nsmutableurlrequest *)request { [self.connection cancel]; nsmutabledata *data = [[nsmutabledata alloc] init]; self.receiveddata = data; self.result = [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding]; nslog(@"receiveddata: %@", [[nsstring alloc] initwithdata:receiveddata encoding:nsasciistringencoding]); self.connection = [[nsurlconnection alloc] initwithrequest:request delegate:self]; if (self.networkerroralert) { nslog(@"connection fail"); } [self.connection start]; }
when typing localhost string in browser im getting data in browser want in code
you need implement delegate:
- (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { nslog(@"received response: %@", [response url]); //give url. [receiveddata setlength:0]; }
when server has provided sufficient data create nsurlresponse object, delegate receives connection:didreceiveresponse: message. delegate method can examine provided nsurlresponse , determine expected content length of data, mime type, suggested filename , other metadata provided server.
edit:
//if want more...you need : nsstring *contenttypevalue = nil; (nsstring *headerkey in [[(nshttpurlresponse*)response allheaderfields] allkeys] ) { if([@"content-type" caseinsensitivecompare:headerkey] == nsorderedsame ) { contenttypevalue = [[(nshttpurlresponse*)response allheaderfields] valueforkey:headerkey]; } } nslog(@"===>%@",contenttypevalue);
Comments
Post a Comment