我正在尝试从我的 iOS 应用程序向服务器发送一个发布请求。该请求在使用 curl 时有效:
curl -d "uid=1&type=robbery&x=41.66505&y=-93.73046" http://thawing-ravine-5632.herokuapp.com/sos
这是发出请求的 Objective-C 代码:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL
URLWithString:@"http://thawing-ravine-5632.herokuapp.com/sos"]];
[request setHTTPMethod:@"POST"];
[request setValue:x
forHTTPHeaderField:@"x"];
[request setValue:y
forHTTPHeaderField:@"y"];
[request setValue:type
forHTTPHeaderField:@"type"];
[request setValue:uid
forHTTPHeaderField:@"uid"];
NSURLConnection * postOutput =[[NSURLConnection alloc]
initWithRequest:request
delegate:self];
最后, didReceiveData 记录以下内容:
2013-03-31 20:29:32.248 THST Mobile[14720:11c03] connectionDidReceiveData {"student_found":0,"school_found":0,"officer_found":0,"text_sent":0,"uid":0,"x":0.0,"y":0.0,"type":null}
但是在发出请求之前记录变量表明它们确实在发出请求之前在应用程序中实例化。通过 SSH 连接到服务器表明在收到请求时它们都只是 nil 或空字符串。所以我的Objective-C代码发出post请求有问题——可能是我设置参数的方式?