2

我从 iOS 调用 Web API,我调用方法 POST,我不能在 post 方法中传递参数

网络接口:

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

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

}

在 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];

如何在POST方法中传递值参数Person?

4

1 回答 1

2
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];

这是来自这篇博文:http: //www.deanoj.co.uk/ios-development/making-a-http-post-request-with-nsurlconnection/

于 2012-12-26T16:29:43.680 回答