iphone - NSURLRequest: How to change httpMethod "GET" to "POST" -


get method, works fine.

url: http://myurl.com/test.html?query=id=123&name=kkk


i not have concepts of post method. please me.

how can chagne method post method?


url: http://testurl.com/test.html

[urlrequest sethttpmethod:@"post"];

try

nsstring *post = [nsstring stringwithformat:@"username=%@&password=%@",username.text,password.text]; nslog(@"%@",post); nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes];  nsstring *postlength = [nsstring stringwithformat:@"%d",[postdata length]]; nsmutableurlrequest *request = [[[nsmutableurlrequest alloc] init] autorelease]; [request seturl:[nsurl urlwithstring:[nsstring stringwithformat:@" server link here"]]]; [request sethttpmethod:@"post"]; nsstring *json = @"{}"; nsmutabledata *body = [[nsmutabledata alloc] init]; [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"current-type"]; [request sethttpbody:postdata]; //get response nshttpurlresponse* urlresponse = nil;   nserror *error = [[nserror alloc] init];   nsdata *responsedata = [nsurlconnection sendsynchronousrequest:request returningresponse:&urlresponse error:&error];   nsstring *result = [[nsstring alloc] initwithdata:responsedata encoding:nsutf8stringencoding]; nslog(@"response code: %d", [urlresponse statuscode]);  if ([urlresponse statuscode] >= 200 && [urlresponse statuscode] < 300)  {     nslog(@"response: %@", result); } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -