2012-12-26 5 views
2

j'appeler API web à partir de iOS, j'appelle la méthode POST, je cann't passer le paramètre dans la méthode postappel service Web reposant dans iOS

API Web:

public class Person 
{ 
    public int Id { get; set; } 
    public String Name { get; set; } 
} 

// POST api/values 
public void Post([FromBody]Person value) 
{ 

} 

dans iOS

NSURL *url =[NSURL URLWithString:@"http://localhost:9281/api/values"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
[theRequest setHTTPMethod:@"POST"]; 

NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; 

Comment passer le paramètre value Person dans POST méthode?

+0

Dans l'avenir, vous voudrez peut-être examiner cette question pour quelque chose comme ceci: https://github.com/AFNetworking/AFNetworking – Joe

Répondre

3
NSMutableURLRequest *request = [NSMutableURLRequest 
            requestWithURL:[NSURL URLWithString:@"http://www.domain.com/your/servlet"]]; 

NSString *params = [[NSString alloc] initWithFormat:@"foo=bar&key=value"]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 
[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

C'est de ce blog: http://www.deanoj.co.uk/ios-development/making-a-http-post-request-with-nsurlconnection/