Http GET and POST method in ios - ios

i need to post my value to the test WebView i try'd but my value didnt post and no change while iam running
NSString *post = [NSString stringWithFormat:#"643c30f73a"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"XXXXX.com"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

Are you implementing the required delegate methods for NSURLConnection?
See the documentation at https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html for details and example implementations.
Implementing the delegate methods will at least give you an indication of any error that is occurring.

its worked for me,
NSURL *url = [NSURL URLWithString: #"http://your_url.com"];
NSString *body = [NSString stringWithFormat: #"arg1=%#&arg2=%#", #"val1",#"val2"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];

Related

HTTPMethod and HTTPBody properties not found on NSMutableURLRequest

I am trying to create a POST request. I am creating a custom URL but when I try and print out values I get the following errors:
EDIT:
(lldb) po request.HTTPBody
error: property 'HTTPBody' not found on object of type 'NSMutableURLRequest *'
(lldb) po request.HTTPMethod
error: property 'HTTPMethod' not found on object of type 'NSMutableURLRequest *'
Here is my code:
NSString *post = [NSString stringWithFormat:#"&serviceTicket=%#&tags=%#&tags=%#",taskId,fullName,#"test1"];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:webUrl]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(#"Connection Successful");
}
else
{
NSLog(#"Connection could not be made");
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:webUrl]];
What am I missing?

NSURLConnection post request stops posting after some time

I am trying to post some values to my web service which is working for android version nicely and for iOS it works for about 12 requests then it stops sending. What am i doing wrong?
I am sending about 1 per second.
-(void) PostDataToServer:(NSMutableArray*) value
{
NSString *xValue = [NSString stringWithFormat:#"%#", [value objectAtIndex:0]];
NSString *yValue = [NSString stringWithFormat:#"%#", [value objectAtIndex:1]];
NSString *jsonRequest = [NSString stringWithFormat:#"{\"person\":\"%#\",\"x\":\"%#\",\"y\":\"%#\",\"z\":\"%#\"}",#"1",xValue ,yValue ,#"0.0"];
NSLog(#"Request: %#", jsonRequest);
NSURL *url = [NSURL URLWithString:#"mywebserviceURL"];
request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody: requestData];
NSData *urlData= connection= [NSURLConnection connectionWithRequest:request delegate:self];
NSLog(urlData);

Web services for iOS application with C# using mysql?

I am working on iPhone application with web services.
Using HTTP POST method I tried post the data. But data is saving in database. The response I'm getting is 404 or 400. here C# is used instead of PHP.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#""]];
NSString *userUpdate =[NSString stringWithFormat:#"Name=%#,FirstName=%#,DateofBirth=%#,Sex=%#,Country=%#,City=%#,PhoneNumber=%#,IncumbentonPremimum=%#",Name,FirstName,DateofBirth,Sex,Country,City,PhoneNumber,IncumbentonPremimum];
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:#"POST"];
NSString *postLength = [NSString stringWithFormat:#"%d",[data1 length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:data1];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
//This is for Response
NSLog(#"got response==%#", resSrt);`

Miss parameter in http post

NSString * aToken = [[[NSUserDefaults standardUserDefaults] arrayForKey:#"accessToken"] firstObject];
NSString * post = [NSString stringWithFormat:#"code=%#", #"035571"];
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString * postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"url"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[request setValue:#"no-cache" forHTTPHeaderField:#"Cache-Control"];
[request setValue:[NSString stringWithFormat:#"Bearer %#", aToken] forHTTPHeaderField:#"Authorization"];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[conn start];
the error is: {"error":"invalid_request","error_description":"Required field is missing. 'code'"}
how to fix it? any idea?

How to send data in url to server?

http://novatoresols.com/demos/blow/users/add.json?json={"email":"ali"}
How can I send a key and data to web? Here "email" is the key and "ali" is the value in URL.
Code:
NSURL *url=[NSURL URLWithString:[hostURL stringByAppendingFormat:#"users/add.json?json="]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString * params =#"{\"email\":\"Ali\"}";
[request setHTTPMethod:#"POST"];
// This is how we set header fields
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Simply Try with this :
NSString * params =#"{\"email\":\"Ali\"}";
NSURL *url=[NSURL URLWithString:[hostURL stringByAppendingFormat:#"users/add.json?json=%#",params]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog (#"%#",data);

Resources