我认为标题解释了它。我可以很好地达到我的api控制器上的Get功能。我可以到达Post方法,但是我的参数(macAddress)为null。我试过这个代码的许多变化在Xcode:IOS到REST-ful MVC Api服务 - [Get]有效,但参数没有通过[Post] 0123到达服务器
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",baseURL,controller]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"macAddress=testestest";
NSData *myRequestData = [ NSData dataWithBytes: [ postString UTF8String ] length: [ postString length ] ];
[request setHTTPBody:myRequestData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
和控制器:
public String Post([FromBody]string macAddress)
{
//......
}
(我知道,我使用的同步请求和响应零/错误,只是想找出这方面)
感谢您的帮助。
只需对代码快速发表评论。字符串可以更简单的方式转换为数据 - [body dataUsingEncoding:NSUTF8StringEncoding] – 2013-02-19 20:05:27
顺便说一句代码看起来不错。我在我的应用程序中有类似的,它工作完美:) – 2013-02-19 20:07:16
您需要安装嗅探器并检查来自客户端的传出流量。我不认为这个问题在客户端。 – 2013-02-19 20:08:42