使用JSON在iOS中发送HTTP POST请求
所以我目前使用Facebook登录我的iOS应用程序,并且服务器管理员最近通过要求Facebook访问令牌以及通过https访问所有连接来添加安全身份验证。 这里是我尝试运行的代码,但无论我做什么,我都得到了500(服务器内部错误)的服务器响应错误。 有任何想法吗?
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:@"https://XXXXXXXXXXXXXXXX/users.json"]];
NSDictionary *requestData = [[NSDictionary alloc] initWithObjectsAndKeys:
userID, @"facebook_id",
FBAccessToken, @"fb_token",
userName, @"name",
nil];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:requestData options:0 error:&error];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
以下是您如何记录响应主体。
@property (strong, nonatomic) NSMutableData *responseData;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.responseData = [NSMutableData data];
…
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
…
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"response data - %@", [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]);
…
}
您还需要符合NSURLConnectionDelagate协议。
链接地址: http://www.djcxy.com/p/70097.html